Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CakePHPで深いJoinが必要な場合は、Containableを使うよりも確実に以下のTipsに従ったほうがいい。確実でしかもSQLもシンプルになり、早い。 Via http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations
CakePHP Behaviouer 'Containable' is very useful,but it's not suitable plugin for fetch deep join records.
I want to join 4 Tables as such.
OrderEntryDetail->Item->Maker->Category
Containable runs sql for each model(equivalent each table) but this tricky BindModel/UnBindModel usage solves this problem.
<Before>
<?php
$OrderEntryDetail->find('first', array(
'conditions' => array('Item.id' => $itemId),
'contain' => array('Item' => array('Maker' => array('Category')))
));
?>
<After>
<?php
$OrderEntryDetail->unbindModel(array(
'belongsTo' => array('Item')
));
$OrderEntryDetail->bindModel(array(
'hasOne' => array(
'Item' => array(
'foreignKey' => false,
'conditions' => array('OrderEntryDetail.item_id = Item.id')
),
'Maker' => array(
'foreignKey' => false,
'conditions' => array('Item.maker_id = Maker.id')
),
'Category' => array(
'foreignKey' => false,
'conditions' => array('Item.category_id = Category.id')
),
)
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.