来自多个表的cakephp数据联接查询

来自多个表的cakephp数据联接查询,cakephp,cakephp-2.0,cakephp-1.3,Cakephp,Cakephp 2.0,Cakephp 1.3,我正在开发一个CakePHP2.x。我有这样一个问题: $this->bindModel(array( 'belongsTo' => array( 'Contact' => array( 'className' => 'Contact', 'foreignKey' => false, 'conditions' => arr

我正在开发一个CakePHP2.x。我有这样一个问题:

  $this->bindModel(array(
        'belongsTo' => array(
            'Contact' => array(
                'className' => 'Contact',
                'foreignKey' => false,
                'conditions' => array(
                    'Message.user_id = Contact.user_id',
                            array('Message.mobileNo' => array('Contact.mobileNo',
                           'Contact.workNo',
                            'Contact.homeNo',
                             'Contact.other')),
                ),

               'order'=>'Message.idTextMessage DESC',
            )
        )
    ), false);

    return $this->find('all', array('conditions' => array('Message.User_id' => $userid),
        'contain' => array('Contact' ),
        'fields' => array('Message.mobileNo',
            'Contact.mobileNo',
            'Contact.workNo',
            'Contact.homeNo',
            'Contact.other',
            'Contact.name',
            'Message.dateTime',
            'Message.type',
            'Message.body'),
        'group' => 'Message.mobileNo',
        'limit' => 6));
}
查询未按预期工作。我发现了问题所在。问题是当我打印这个查询时。它在('Contact.mobileNo')周围添加了一个引号(“”),就像这样 和('Contact.mobileNo')中的
消息
mobileNo
, “Contact.workNo”, “联系人,homeNo”, ‘Contact.other’)

因此,当我删除SQL yog中的引用时。查询是有效的。我的意思是,我认为这部分中的联系人没有找到mobileno、workno等。有人知道我该怎么做吗

如果您想看到简单的sql查询,它工作得非常完美。。在这儿吗

          SELECT Message.mobileNo,
        Contact.mobileNo,
        Contact.workNo,
        Contact.homeNo,
        Contact.other,
        Contact.name,

        Message.body,
        Message.idTextMessage
   FROM cakephp_db.textmessage AS Message
   LEFT JOIN cakephp_db.contacts AS Contact ON Message.user_id = Contact.user_id
                                     AND Message.mobileNo IN (Contact.mobileNo,           Contact.workNo, Contact.homeNo, Contact.other)
 WHERE Message.User_id = 23
 GROUP BY Message.mobileNo
ORDER BY Message.idTextMessage DESC LIMIT 6

您应该将其更改为:

$this->bindModel(array(
        'belongsTo' => array(
            'Contact' => array(
                'className' => 'Contact',
                'foreignKey' => false,
                'conditions' => array(
                    'Message.user_id = Contact.user_id',
                    '`Message`.`mobileNo` IN (`Contact`.`mobileNo`,`Contact`.`workNo`,`Contact`.`homeNo`,`Contact`.`other`)'),
                'order'=>'Message.idTextMessage DESC',
            )
        )
    ), false);

当您使用条件的键值时,蛋糕将值视为字符串值而不是列名。