使用bindModel方法Cakephp连接CakePHP中的两个表

问题描述 投票:0回答:1

我正在开发 cakephp 2.x ..我的数据库中有两个表都有 userid 我正在使用 bindModel..我的查询工作正常...我只有一个问题 ..我想要在 where 子句中添加条件

  **where userid = $userid** 


 function getMessages($userid){
     $this->bindModel(array(
        'belongsTo' => array(
        'Contact' => array(
            'className' => 'Contact',
            'foreignKey' => false,
            'conditions' => array(

                'Message.user_id = Contact.user_id',
                'AND' =>
                array(
                    array('OR' => array(
                        array('Message.mobileNo = Contact.mobileNo'),
                        array('Message.mobileNo = Contact.workNo'),

                    )),


                )


            ),
            'type' => 'LEFT',


        )
    )
), false);

return $this->find('all', array('conditions' => array(),
    'fields' => array('Message.mobileNo'


    ),
    'group' => 'Message.mobileNo',
    'limit' => 6));


          }

我在参数中获取用户 ID ...所以我想添加获得以下结果的条件,其中

    message.userid and contact.userid = $userid ...
cakephp cakephp-2.0 cakephp-2.1 cakephp-model
1个回答
0
投票

只需将条件分成两行,例如:

'conditions' => array(
    'Contact.user_id' => $userid,
    'Message.user_id = Contact.user_id',
    [...]
 )

但是更有意义的方法是保持绑定不变 - 毕竟绑定通常发生在具有相同 id 的消息用户和联系人用户之间 - 并在

find('all')
中使用
'Message.user_id' => $userid
添加特定情况的条件.

© www.soinside.com 2019 - 2024. All rights reserved.