Reputation: 2215
For the purpose of this question, this is the association tree (all ->
means hasMany
), all database structure code adheres to CakePHP conventions.
Forum -> Section (forum_id) -> Topic (section_id) -> Reply (topic_id)
I'd like to run a $this->Reply->find
query with certain conditions, and I would like the returned $data["Reply"]
array to only return replies where they belong to forum_id=X
.
For example, I run a $this->Reply->find
with certain conditions (these don't matter), and it returns two results with different parents, and when you go up and up until you reach Forum.id
(or Section.forum_id
), they differ in forum_id
.
What I want is to filter the results so they belong to a certain forum_id
. Since forum_id
is not a field in the Reply
model itself, but instead in Section
(which is two "layers" up), I can't use a conditions
entry to filter the result.
What should I do?
Upvotes: 0
Views: 95
Reputation: 6767
Simple as this:
<?php
$this->Reply->find('all', array(
'joins' => array(
Reply::joinLeft('Topic'),
Topic::joinLeft('Section'),
Section::joinLeft('Forum'),
),
'conditions' => array('Forum.id' => $forumId),
));
https://github.com/tigrang/EasyJoin - This will determine the relationship between the models and create the join arrays for you.
If you don't want to use the plugin, you'll have to specify the joins array manually or rebind the models to be able to use Containable as it would currently create multiple queries rather than joins.
Upvotes: 2