Reputation: 506
I set up my models and and the following find() returns me the array underneath.
query:
find('all', array(
'conditions' => array(
'Teamstarter.season' => $season),
'contain' => array(
'Teamstarter.Fflteam')
)
);
result:
Array
(
[0] => Array
(
[Score] => Array
(
[id] => 1
[teamstarter_id] => 1
[roster_id] => 1
[score] =>
[position] => QB
)
[Teamstarter] => Array
(
[id] => 1
[season] => 2013
[gameday] => 1
[fflteam_id] => 1
[is_flex] => 0
[Fflteam] => Array
(
[id] => 1
[user_id] => 1
[active] => 1
[name] => testteam11
[league_id] => 1
)
)
)
Now I want to add a condition for the league_id in the Fflteam Model. I tried to add
'Teamstarter.Fflteam.league_id' => $league
to the conditions array, but then I get an sql error that the column 'Teamstarter.Fflteam.league_id' could not be found. How do I add a condition refering to fields in deeper levels.
Assosiations should be set correctly since the Models are found.
Thanks for the help.
EDIT
The Suggestion below just removes the Fflteam part
'contain' => array(
'Teamstarter.Fflteam' => array(
'conditions' => array(
'Fflteam.league_id' => $league
)
)
)
result:
Array
(
[0] => Array
(
[Score] => Array
(
[id] => 1
[teamstarter_id] => 1
[roster_id] => 1
[score] =>
[position] => QB
)
[Teamstarter] => Array
(
[id] => 1
[season] => 2013
[gameday] => 1
[fflteam_id] => 1
[is_flex] => 0
[Fflteam] => Array
(
)
)
)
But i don't want it to find the whole array entry. In this case for example if the league_id is 2 , the array should be empty not only the Fflteam part.
Upvotes: 0
Views: 884
Reputation: 506
I found the solution in another thread:
CakePHP 2.x containable behavior conditions on deep associations
Here is the link which was in there:
http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations
And here the code I used:
$this->unbindModel(array(
'belongsTo' => array('Teamstarter')
));
$this->bindModel(array(
'hasOne' => array(
'Teamstarter' => array(
'foreignKey' => false,
'conditions' => array('Teamstarter.id = Score.teamstarter_id')
),
'Fflteam' => array(
'foreignKey' => false,
'conditions' => array('Fflteam.id = Teamstarter.fflteam_id')
)
)));
find('all', array(
'conditions' => array(
'Teamstarter.season' => $season,
'Fflteam.league_id' => $league),
'contain' => array(
'Teamstarter',
'Fflteam')
));
Upvotes: 2
Reputation: 2968
Try some like this:
'contain' => array(
'Teamstarter.Fflteam' => array(
'conditions' => array(
'Fflteam.league_id' => $league
)
)
),
Or read great article: http://nuts-and-bolts-of-cakephp.com/2008/09/05/example-of-cakephps-containable-for-deep-model-bindings/
Upvotes: 0