我正在开发 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 ...
只需将条件分成两行,例如:
'conditions' => array(
'Contact.user_id' => $userid,
'Message.user_id = Contact.user_id',
[...]
)
但是更有意义的方法是保持绑定不变 - 毕竟绑定通常发生在具有相同 id 的消息用户和联系人用户之间 - 并在
find('all')
中使用 'Message.user_id' => $userid
添加特定情况的条件.