本文介绍了在cakephp中找到关于hasMany关系的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的项目中为消息搜索工作,这里有两个模型:Msgcontent和Msgblock.Relationship是Msgblock hasMany Msgcontent.With我想做的是获取所有的Msgblock记录包含Msgcontent与一些搜索我的代码如下:
I'm working an action for message search in my project,and here are the two models:Msgcontent and Msgblock.Relationship is Msgblock hasMany Msgcontent.What I want to do is get all the Msgblock records that contain Msgcontent with some searching keyword.My code as follows:
if($keyword)
{
$conditions['and'] = array(
'Msgcontent.content LIKE'=>'%'.$keyword.'%',
'Msgcontent.content <>'=>''
);
$results = $this->Msgblock->Msgcontent->find('all',array('group'=>array('Msgblock.chatsessionid'),'conditions'=>$conditions));
}
这似乎不是一个好工作。 / p>
It seems not a good work.Is there any better solution?Thanks.
推荐答案
使用适当的JOIN编写自己的SQL查询,这是关于在Cake中使用两个查询的最简单的方法:
Short of writing your own SQL query with appropriate JOINs, this is about the easiest way to do it in Cake with two queries:
$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))
));
这篇关于在cakephp中找到关于hasMany关系的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!