Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:44:35+00:00 2026-06-17T18:44:35+00:00

Score belongs to Player : class Score extends AppModel { public $name = ‘Answer’;

  • 0

Score belongs to Player:

class Score extends AppModel {
    public $name = 'Answer';
    public $belongsTo = array('Player');
}

In PlayersController, I want to get player scores, with his details.

Question #1: How to include belongsTo model in find method result? (join it)

Question #2: How to get sum of all scores distance (Score.distance) which belongs to that player? (I mean SUM(Score.distance), group by Score.player_id)

Note for Q1: Because each player has a lot scores, I don’t like to join scores in each find method I use in that controller. I want to get them in just 1 action)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:44:36+00:00Added an answer on June 17, 2026 at 6:44 pm

    Cakephp automagic not actually join tables as you can see in sql_dump in your layout at bottom.

    You will see below queries.

    SELECT `Player`.`id`, `Player`.`name`, `Player`.`created` FROM `players` AS `Player` WHERE `Player`.`id` = 10 GROUP BY `Player`.`id`
    
    SELECT `Score`.`id`, `Score`.`player_id`, `Score`.`scores` FROM `scores` AS `Score` WHERE `Score`.`player_id` = (10) 

    Quite clear that its not actually join table so you need to do it following way.

    And following will help you because i have already tested it.

    In Player Controller you need to unbind score model first and then custom code to join score table as below.

    In player model create one virtual field as below.

    <?php
    class Player extends AppModel
    {
        var $name = 'Player';
        var $displayField = 'name';
    
        var $virtualFields = array
        (
            'Distance' => 'SUM(Score.scores)'
        );
    
        var $hasMany = array
        (
            'Score' => array
            (
                'className' => 'Score',
                'foreignKey' => 'player_id',
                'dependent' => false,
                'conditions' => '',
                'fields' => '',
                'order' => '',
                'limit' => '',
                'offset' => '',
                'exclusive' => '',
                'finderQuery' => '',
                'counterQuery' => ''
            )
        );
    }
    ?>

    For testing i manually added conditions to find player number 10 scores

    <?php
    class PlayersController extends AppController {
    
        var $name = 'Players';
    
        function index()
        {
    
            $this->Player->unbindModel(array
            (
                'hasMany' => array
                (
                    'Score'
                )
            ));
    
            $this->Player->bindModel(array
            (
                'belongsTo' => array
                (
                    'Score' => array
                    (
                        'foreignKey' => false,
                        'conditions' => array
                        (
                            'Player.id = Score.player_id'
                        )
                    )
                )
            ));
    
            $order = "Player.id";
                    $direction = "asc";
                    if(isset($this->passedArgs['sort']) && $this->passedArgs['sort']=="Distance")
                    {
                            $order = $this->passedArgs['sort'];
                        $direction = $this->passedArgs['direction'];
                        unset($this->passedArgs['sort']);
                        unset($this->passedArgs['direction']);
                    }
    
                    $this->pagination = array();
    
                    $this->pagination = array
                    (
                        'conditions' => array
                        (
                         'Score.player_id' => 10
                        ),
                        'fields' => array
                        (
                            'Player.*',
                            'SUM(Score.scores) AS Distance'
                        ),
                        'group' => array
                        (
                            'Score.player_id'
                        ),
                        'limit' => 15,
                            'order'=>$order." ".$direction
                   );
    
                  $playersScore = $this->paginate('Player');
        }   
    }
    ?>

    And the resulting array would be looks like below.

    Array
    (
        [0] => Array
        (
            [Player] => Array
            (
                [id] => 10
                [name] => Dexter Velasquez
                [created] => 2012-08-02 12:03:07
                [Distance] => 18
            )
        )
    )

    For testing i have used Generate Mysql Data.

    for sorting link

    <?php $direction = (isset($this->passedArgs['direction']) && isset($this->passedArgs['sort']) && $this->passedArgs['sort'] == "Distance" && $this->passedArgs['direction'] == "desc")?"asc":"desc";?>

    and display above link as below.

    <?php echo $this->Paginator->sort('Distance','Distance',array('direction'=>$direction));?>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object called Player with properties such as score, turns, name, etc.
I have a value int called score and i want to save this to
I need to combine name scope with or operator... Something like: class Product <
My Employee have a Name, an Outlet he must score and he must give
I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many
I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number
I am using Postgresql and my player model has some associations such as: class
I have the following scope for finding a product that belongs to a specific
My score increment is not working. Two questions. Will it automatically update? Or is
I have a score i need to calculate for multiple items for multiple users.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.