Search In Post Or In Comments, Find Both

Hi,

I’ve got a table and model Post.

A Post can have many comments, so in Post i’ve got the relation:


'comment' => array(self::HAS_MANY, 'Comment','post_id')

Now I have a search function. I want to search for “foo” and when it’s in the Post title, I want them to show up.

But also when it’s in a comment I want the Post to show up and the comment containing the search term too.

So I tried:




$criteria = new CDbCriteria();

$criteria->alias = 'post';

$criteria->compare('post.title',$searchterm,true,'OR');

$criteria->compare('comment.title',$searchterm,true,'OR');

$criteria->with = 'comment';

$criteria->together = true;				

$criteria->group = 'post.id';

$criteria->limit = 20;		

$posts = Post::model()->findAll($criteria);



When I search for “foo” and it’s in a Post title, this shows up, but it also gets all comments from that post. No matter if they matched or not.

So I want:

  • Only a Post when the search term match it’s title.

  • The comment(s) and the related Post when the search term match in the comment.

I can’t figure it out. Any advice?