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)
Cakephp automagic not actually join tables as you can see in sql_dump in your layout at bottom.
You will see below queries.
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.
For testing i manually added
conditionsto find player number10 scoresAnd the resulting array would be looks like below.
For testing i have used Generate Mysql Data.
for sorting link
and display above link as below.