简体   繁体   English

CakePHP在查找查询中连接表

[英]CakePHP join tables in find query

I have Workposition model. 我有Workposition模型。 It is linked in database with Orders with belongsTo relationship. 它在数据库中与具有belongsTo关系的Orders链接。 So, I need to find specific workpositions bz the conditions related to Orders model. 因此,我需要找到与Orders模型相关的条件的特定工作位置。 So, When I use for example suck kind of find: 所以,当我使用例如suck有点找到:

$workpositions = $this->Workposition->find('all', array(
                'conditions' => array(
                        'Order.type' => 'N'
                )
));

CakePHP understand the Order.id notation. CakePHP理解Order.id表示法。 But when i'm trying to use joins tables: 但是当我尝试使用连接表时:

$workpositions = $this->Workposition->find('all', array(
                'conditions' => array(
                        'Order.type' => 'N'
                )
                'joins' => array(
                        array('table' => 'ordergroups_orders',
                            'alias' => 'OrdergroupsOrder',
                            'type' => 'INNER',
                            'conditions' => array(
                                    'Order.id = OrdergroupsOrder.order_id',
                                    'OrdergroupsOrder.ordergroup_id' => '3',                                    
                            )
                    )               
        )));

It gives me an error : Column not found: 1054 Unknown column 'Order.id' in 'on clause' . 它给了我一个错误: Column not found: 1054 Unknown column 'Order.id' in 'on clause' So it doesn't understand Order.id notation. 所以它不理解Order.id表示法。 What can be the problem ? 可能是什么问题?

I tried also to make something like this : 我也试着做这样的事情:

$workpositions = $this->Workposition->find('all', array(
                'conditions' => $conditions,
                'joins' => array(
                    array('table' => 'orders',
                        'alias' => 'Orders',
                        'type' => 'INNER',
                    ),
                    array('table' => 'ordergroups_orders',
                            'alias' => 'OrdergroupsOrder',
                            'type' => 'INNER',
                            'conditions' => array(
                                    'Orders.id = OrdergroupsOrder.order_id',
                                    'OrdergroupsOrder.ordergroup_id' => $ordergroup_ids,                                    
                            )
                    )   
        )));

But i get error Column not found: 1054 Unknown column 'Array' in 'on clause'(Array to string conversion). 但我收到错误列找不到:1054'on子句'中的未知列'Array'(数组转换为字符串)。 So it doesn't undestand my array of ids, while it undestands it without the binding of Order model, when the find Method sees Order. 因此,当find方法看到Order时,它没有取消我的id数组,而它没有绑定Order模型。

The join conditions must be the array value, not key => value. 连接条件必须是数组值,而不是key => value。

Try change the line 尝试更改线路

'OrdergroupsOrder.ordergroup_id' => $ordergroup_ids, 

to

'OrdergroupsOrder.ordergroup_id = $ordergroup_ids',

$ordergroup_ids is an Array? $ ordergroup_ids是一个数组? Try to use a single id. 尝试使用单个ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM