A
A
Alexander2015-10-12 13:25:35
Yii
Alexander, 2015-10-12 13:25:35

How to find out which article has the most comments?

How can I find out which article has the most comments?

ArticleController : 
$discussedArticle = Articles::find()->with('comments')->groupBy('article_id')->orderBy('count(article_id) DESC')->asArray()->all();
Model : article
public function getComments()
    {
        return $this->hasMany(Comments::className(), ['article_id' => 'id']);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-10-12
@kentuck1213

Found :D
$discussedArticle = Articles::find()->joinWith('comments', true, 'RIGHT JOIN')->groupBy('comments.article_id')->orderBy('count(comments.article_id) DESC' )->limit(5)->all();

D
Doniyor Mamatkulov, 2015-10-12
@doniyorbekm

select MAX(id) from your_table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question