【问题标题】:CakePHP join tables in find queryCakePHP 在查找查询中连接表
【发布时间】:2017-03-24 10:04:03
【问题描述】:

我有 Workposition 模型。它在数据库中与具有 belongsTo 关系的 Orders 链接。所以,我需要找到与订单模型相关的具体工作岗位。因此,当我使用例如吮吸式查找时:

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

CakePHP 理解 Order.id 符号。但是当我尝试使用连接表时:

$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',                                    
                            )
                    )               
        )));

它给了我一个错误:Column not found: 1054 Unknown column 'Order.id' in 'on clause'。所以它不理解Order.id 表示法。可能是什么问题?

我也尝试过制作这样的东西:

$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,                                    
                            )
                    )   
        )));

但我得到错误 Column not found: 1054 Unknown column 'Array' in 'on Clause'(数组到字符串的转换)。所以它不会理解我的 id 数组,而当 find 方法看到 Order 时,它会在没有 Order 模型绑定的情况下理解它。

【问题讨论】:

  • 我认为您必须将 Order 模型与此绑定
  • @MdHasiburRahaman,我添加了一些代码。没用

标签: php mysql cakephp cakephp-2.4


【解决方案1】:

连接条件必须是数组值,而不是key => value。

换行试试

'OrdergroupsOrder.ordergroup_id' => $ordergroup_ids, 

'OrdergroupsOrder.ordergroup_id = $ordergroup_ids',

$ordergroup_ids 是一个数组?尝试使用单个 id。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    相关资源
    最近更新 更多