Filtering with conditions on an associations's association in CakePHP -
Filtering with conditions on an associations's association in CakePHP -
in app have burger
model, joint
model, , location
model.
joints have many burgers, , have many locations.
what want filter burgers 2 ways: based on rating, , bases on whether belong joint has location matches condition, in case, city.
here's i've tried, no workey:
$burgers = $this->burger->find('all', array( 'conditions' => array('burger.official_rating !=' => 0), 'contain' => array( 'joint', 'joint.location.conditions' => array( 'location.city' => 'toronto' ) ), 'order' => array('burger.official_rating desc'), 'limit' => 10 ));
joint.location.conditions
seems have no effect.
edit: using joins, unsure if efficient way (notice, had add together contain clause, create sure associated info returned in results).
$options['joins'] = array( array('table' => 'joints', 'alias' => 'joint1', 'type' => 'inner', 'conditions' => array( 'joint1.id = burger.joint_id' ) ), array('table' => 'locations', 'alias' => 'location2', 'type' => 'inner', 'conditions' => array( 'location2.joint_id = burger.joint_id' ) ) ); $options['conditions'] = array( 'location2.city' => 'toronto', 'burger.official_rating !=' => 0 ); $options['order'] = array( 'burger.official_rating desc', ); $options['limit'] = 10; $options['contain'] = array( 'joint','joint.location' ); $burgers = $this->burger->find('all', $options);
problem:
this mutual issue people have, remedied understand you cannot limit main find model based on conditions against it's contained items.
the reason can't because doing contain creates separate queries on tables, opposed join, creates single query.
solution:
your best bet in case utilize cakephp joins.
in cases (not yours), alternate using joins alter model you're doing query on. if need 1 condition, alter find model status instead of 1 within contain , contain in reverse order.
cakephp associations
Comments
Post a Comment