YII - 加载所有has_many记录接受条件的所有父记录

时间:2015-04-27 07:15:13

标签: php yii

在下面的代码中,这将加载所有作者的一个帖子,其中包含post.title ='search'

// query criteria
$criteria = new CDbCriteria();
// with Post model
$criteria->with = array('posts');
// compare title
$criteria->compare('posts.title', $searchWord, true);
// find all authors
$authors = Author::model()->findAll($criteria);
// show all authors and his/her posts
foreach($authors as $author)
{
    echo "Author = " . $author->name . "\n";
    foreach($author->posts as $post)
    {
        echo "Post = " . $post->title . "\n";
    }
}

但我需要的只是加载那些所有帖子都接受条件的作者。我在这里使用分页,所以我想做一个发现声明。

我浏览了很多,但可以找到的是HAS_MANY记录的急切加载和延迟加载,但我需要的是不同的。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

如何通过帖子而非作者搜索?

// query criteria
$criteria = new CDbCriteria();
// with author model
$criteria->rightJoin('author', 'author.id=post.author_id')
// compare title
$criteria->compare('title', $searchWord, true);

//optional: group by author
$criteria->group = 'author.id';

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

// show all post with theirs author
foreach($posts as $post)
{
    echo "Post = " . $post->title . "\n";
    echo "Author = " . $post->author->name .  "\n";
}

答案 1 :(得分:0)

这将为作者提供满足条件的所有帖子

// query criteria
$criteria = new CDbCriteria();
// with Post model
$criteria->with = array('posts');
// compare title
$criteria->compare('posts.title', $searchWord, true);

$criteria->select = ['*',new CDbExpression('(SELECT COUNT(id) FROM posts WHERE author_id = t.id) AS all_posts'),new CDbExpression('COUNT(posts.id) AS post_count')];

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

$criteria->distinct = true;

$criteria->having = 'all_posts = post_count';

// find all authors
$authors = Author::model()->findAll($criteria);
// show all authors and his/her posts
foreach($authors as $author)
{
    echo "Author = " . $author->name . "\n";
    foreach($author->posts as $post)
    {
        echo "Post = " . $post->title . "\n";
    }
}

答案 2 :(得分:-1)

该解决方案要求您查询具有所需帖子标准的作者,然后检查这些作者是否没有任何帖子不满足标准。

在简单的sql中,这意味着像这样的

select * from author where id in (select author from post where post.title like '%word%') and id not in (select author from post where postcriteria not like '%word%')

在Yii中实现此功能需要db query,示例查询将是

Yii::app()->db->createCommand()
->select('*')
->from('author') 
->where('id in (select author from post where post.title like %:word%) 
 and id not in (select author from post where post.title not like %:word%)', array(':word'=>$word))
->queryRow();