Mysql CakePHP2.x:加入belongsto
在我的“mesn”模型中,我有以下关系:Mysql CakePHP2.x:加入belongsto,mysql,cakephp,join,belongs-to,Mysql,Cakephp,Join,Belongs To,在我的“mesn”模型中,我有以下关系: public $belongsTo = array( 'Order' => array( 'className' => 'Order', 'foreignKey' => 'order_id', 'conditions' => '', 'fields' => '', 'order' => '' ), 'SaleOrder' => array(
public $belongsTo = array(
'Order' => array(
'className' => 'Order', 'foreignKey' => 'order_id', 'conditions' => '', 'fields' => '', 'order' => ''
), 'SaleOrder' => array(
'className' => 'SaleOrder', 'foreignKey' => 'sale_order_id', 'conditions' => '', 'fields' => '',
'order' => ''
), 'ShippedBox' => array(
'className' => 'Box', 'foreignKey' => 'shipped_box_id', 'conditions' => '', 'fields' => '',
'order' => ''
)
); /* * * hasMany associations** @var array
在我的一个模型函数中,我想加入“belongsto”“ShippedBox”(表:box)上的另一个表。但无论我如何尝试编写连接,我都会收到未知列的错误消息:
$arr_s_result = $this->find('all', array(
'joins' => array(
array('table' => 'shipments',
'alias' => 'MyShipments',
'type' => 'INNER',
'conditions' => array(
'MyShipments.id = ShippedBox.shipment_id'
)
)),
'conditions' => array('Mesn.name' => $arr_search), 'recursive' => 0
));
我试过:
'MyShipments.id = ShippedBox.shipment_id'
及
甚至
'MyShipments.id = Boxes.shipment_id'
其中存在带有“装运id”字段的表格“箱子”
我怎样才能让这个连接工作
'MyShipments.id = box.shipment_id'
致:
还有这个呢:
$arr_s_result = $this->find('all', array(
'conditions' => array('Mesn.name' => $arr_search), 'recursive' => 0
)))
belongstojoin是自动完成的。我想你需要做一些事情,比如使用子查询来获得你想要的-
$arr_s_result = $this->find('all', array(
'joins' => array(
array('table' => '(SELECT boxes.id, [enter other fields you need here] FROM shipments JOIN boxes ON boxes.shipment_id = shipments .id)',
'alias' => 'MyShipments',
'type' => 'INNER',
'conditions' => array(
'Mens.shipped_box_id = MyShipments.id'
)
)),
'conditions' => array('Mesn.name' => $arr_search), 'recursive' => 0
));
也许你不需要加入?As belongsTo是自动完成的吗?我从执行的sql语句中看到的问题是,它在所有连接的开头添加了连接,在这些连接中,框还不“已知”。这是原始查询,但它没有解决框到发货的依赖关系。这是一个很好的解决方案。将测试明天何时返回工作,并给出进一步的评论或接受
$arr_s_result = $this->find('all', array(
'conditions' => array('Mesn.name' => $arr_search), 'recursive' => 0
$arr_s_result = $this->find('all', array(
'joins' => array(
array('table' => '(SELECT boxes.id, [enter other fields you need here] FROM shipments JOIN boxes ON boxes.shipment_id = shipments .id)',
'alias' => 'MyShipments',
'type' => 'INNER',
'conditions' => array(
'Mens.shipped_box_id = MyShipments.id'
)
)),
'conditions' => array('Mesn.name' => $arr_search), 'recursive' => 0
));