Last active
December 19, 2015 09:19
-
-
Save gothedistance/5931772 to your computer and use it in GitHub Desktop.
CakePHPで深いJoinが必要な場合は、Containableを使うよりも確実に以下のTipsに従ったほうがいい。確実でしかもSQLもシンプルになり、早い。 Via http://mark-story.com/posts/view/using-bindmodel-to-get-to-deep-relations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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