i have 3 model, with this relationships:
class A extends AppModel {
public $belongsTo = array(
"B" => array(
'className' => 'B',
'foreignKey' => 'b_id'
)
);
}
class B extends AppModel {
public $belongsTo = array(
"C" => array(
'className' => 'C',
'foreignKey' => 'c_id'
)
);
}
class C extends AppModel {
}
first, fetch model A records with this code:
$this->A->find('all', array('recursive'=>2));
when print query log, instead of two left join, this query was seen:
SELECT * FROM A LEFT JOIN B ON (A.b_id = B.id)
And for each result record, a query to get the C model information like this seen:
SELECT * FROM C WHERE id = ?
is there a way with a 2 join query to do that?
I've found when CakePHP does your find via multiple queries, you can force it to get all in a single query by using manual joins:
$this->A->find('all', array(
'recursive' => -1,
'joins' => array(
array(
'table' => 'b',
'alias' => 'B',
'type' => 'left',
'conditions' => array('B.id = A.b_id'),
),
array(
'table' => 'c',
'alias' => 'C',
'type' => 'left',
'conditions' => array('C.id = B.c_id'),
),
),
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.