I want to get the user that wrote the most articles. I do so fine in two ways with ActiveRecord like the following:
$table = Articles::find()
->select('articles.*, COUNT(*) AS cnt')
->with('user','userDetails')
->groupBy('articles.user_id')
->orderBy(('cnt DESC'))
->limit(10)
->offset($offset)
->all();
and with a query like the following:
$query = (new Query())
->select('articles.user_id, COUNT(*) AS num_articles')
->from('articles')
->join('LEFT JOIN', 'user_details', 'user_details.user_id = articles.user_id')
->groupBy('articles.user_id')
->orderBy('num_articles DESC')
->limit(10)
->offset($offset)
->all();
- The problem is that the ActiveRecord gives me further needed informations
userDetails
that I need. But I do not get the amount of articles of user that should be oncnt
- With the Query I get the
user_id
and the amount of articles. But I do not get it working by joining withuserDetails
. All of these does not work:LEFT JOIN
,RIGHT JOIN
,INNER JOIN
.
I am interested in resolving both for learning, but for concrete I need help with the ActiveRecord problem.