在CakePHP中操纵JOINS的顺序 [英] Manipulating Order of JOINS in CakePHP
问题描述
我的CakePHP存在以下问题:
I have the following problem with CakePHP:
在我的模型中,存款"属于帐户",帐户属于"客户".
In my model, Deposit belongsTo Account, and Account belongsTo Customer.
在查询存款时,默认情况下,我会获取帐户信息,而不是客户的信息.
When querying Deposits, I get the Account information, but not the Customer's, by default.
如果我将Deposit-> recursive设置为2,我会获得客户信息(以及更多),但是CakePHP本质上每笔存款都会抛出一个SELECT,这在这种情况下是很糟糕的.
If I set Deposit->recursive to 2, I get the Customer information (and a whole lot more), but CakePHP esentially throws one SELECT per each deposit, which is quite bad in this case.
所以,我这样做了:
'joins' => array(
array('table'=>'customers', 'alias'=>'Customer', 'type'=>'left', 'foreignKey' => false, 'conditions'=>array('Account.customer_id = Customer.id'))
)
几乎 有效的
我从中得到的基本上是:
What I get from that is esentially:
SELECT (...) FROM Deposits LEFT JOIN Customers LEFT JOIN Accounts
代替
SELECT (...) FROM Deposits LEFT JOIN Accounts LEFT JOIN Customers
那当然是行不通的.
无论如何,在常规模型连接"之后是否要指定我的自定义连接"?
还是我必须手动取消与帐户的存款绑定,并手动指定两个联接?
Is there anyway to specify that "my custom joins" should go after the "regular model joins"?
Or do I have to manually unbind Deposit from Account, and specify both joins manually?
谢谢!
丹尼尔
Thanks!
Daniel
推荐答案
您可以使用Containable行为来选择所需的内容,因此可以从Deposits控制器中进行选择:
You can use the Containable behavior to select just what you want, so from your Deposits controller:
$this->Deposit->find('all', array(
// conditions
'contain' => array('Account' => array('Customer'))
));
只需确保将actsAs
变量添加到类中.这是更多信息. http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
Just be sure to add the actsAs
variable to the class. Here's more info. http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
这篇关于在CakePHP中操纵JOINS的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!