我正在为我的项目中的消息搜索操作,这里有两个模型:Msgcontent和Msgblock.Relationship是Msgblock hasMany Msgcontent.我想要做的是获取包含Msgcontent的所有Msgblock记录和一些搜索关键字.我代码如下:
if($keyword)
{
$conditions['and'] = array(
'Msgcontent.content LIKE'=>'%'.$keyword.'%',
'Msgcontent.content <>'=>''
);
$results = $this->Msgblock->Msgcontent->find('all',array('group'=>array('Msgblock.chatsessionid'),'conditions'=>$conditions));
}
Run Code Online (Sandbox Code Playgroud)
这似乎不是一个好工作.有没有更好的解决方案?谢谢.
如果没有使用适当的JOIN编写自己的SQL查询,这是使用两个查询在Cake中执行此操作的最简单方法:
$ids = $this->Msgblock->Msgcontent->find('all', array(
'fields' => array('Msgcontent.msgblock_id'),
'recursive' => -1,
'conditions' => ...
));
$this->Msgblock->find('all', array(
'conditions' => array('Msgblock.id' => Set::extract('/Msgcontent/msgblock_id', $ids))
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6378 次 |
| 最近记录: |