CakePHP-如何在联接表上联接表? [英] CakePHP - How do I join a table on a joined table?
问题描述
我在FilesController中,正在尝试根据其顺序属于当前用户的条件来获取文件。
I am in the FilesController and I'm trying to get a file based on the condition that its order belongs to the current user.
FilesController
// Check the file exists and that it belongs to the user
$this->File->find('first', array(
'conditions' => array(
'File.id' => $id,
'Order.Customer.id' => $this->Auth->user('id')
),
'recursive' => 2
));
蛋糕SQL错误
Unknown column 'Order.Customer.id' in 'where clause'
我正在尝试使SQL左联接订单
到个文件
上,然后左联接 customers
到 orders
上,但是我不确定如何加入客户表,尽管我确定以前已经做到了。我已经尝试了所有可以想到的条件,并使用了包含
。
I'm trying to get the SQL to left join orders
onto files
and then left join customers
onto orders
, but I can't figure out how to join the customers table, although I'm sure I've done this before. I've tried everything I can think of with the conditions, and using contains
.
这是我的模型关系:
客户模型
class Customer extends AppModel {
public $hasMany = array('Order');
}
订单模型
class Order extends AppModel {
public $belongsTo = array('Customer');
public $hasMany = array('File');
}
文件模型
class File extends AppModel {
public $belongsTo = array('Order');
}
推荐答案
我意识到在这种情况下,实际上不需要加入客户表,因为订单表已经具有customer_id。
I realised there is no need to actually join the customers table in this instance, because the orders table already has the customer_id.
$this->File->find('first', array(
'conditions' => array(
'File.id' => $id,
'Order.customer_id' => $this->Auth->user('id')
)
));
这篇关于CakePHP-如何在联接表上联接表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!