gpt4 book ai didi

php - 在 yii2 Activerecord 的关系表中获取计数

转载 作者:可可西里 更新时间:2023-11-01 13:40:01 26 4
gpt4 key购买 nike

我有两个用于帖子和用户的表。我想在用户列表 gridview 中显示用户的帖子数。在 yii 1 中,我在模型中使用它来为此目的定义一个关系:

'postCount' => array(self::STAT, 'Post', 'author',
'condition' => 'status = ' . Post::ACTIVE),

...
User:find...().with('postCount').....

但我不知道如何在 Yii2 中实现这个以获取 User:find():with('...') 中的帖子计数以显示在 gridview 中。
有人在 yii2 中尝试过吗?

最佳答案

这是我所做的一个示例,到目前为止它似乎运行良好。它用于计算帖子的评论数。我只是使用了一个标准的事件记录计数,并使用 $this->id 和条目的主键创建了与 where 语句的关系,它得到了一个计数。

public function getComment_count()
{
return Comment::find()->where(['post' => $this->id])->count();
}

只是传递它...

关于php - 在 yii2 Activerecord 的关系表中获取计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26894987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com