Answer the question
In order to leave comments, you need to log in
How to get the number of comments (in 3 identical separate tables) of each post using hasMany?
Good evening everyone!
In general, how to get the number of comments for each entry, if the Comments are in the same table, then in the post model we write the final as well:
public function getComment()
{
return $this->hasMany(Comments::className(),['post_id' => 'id'])->select('id,post_id');
}
1) Таблица - comments_blog
2) Таблица - comments_blog_2
3) Таблица - comments_blog_3
Answer the question
In order to leave comments, you need to log in
In general, everything turned out to be easier than I thought) Here, if anyone is interested.
Since I already had a method that returned the full path to the Class of each individual comment table, then in the Post Class we write this:
public function getComment($postID)
{
return $this->hasMany(ComentsBlogObject::changeDB($postID)::className(),['post_id' => 'id'])->select('COUNT(*)');
}
$model = ArticleBlog::find()->limit(3)->all();
foreach ($model as $key => $value) {
print_r($value->getComment($value->id)->count() );
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question