Hello, I got the tables User, Action and performed_action. When a user performs a action it gets submitted to the performed_action table. I wanna retrieve the data like this query does:
SELECT action_id, COUNT(*) as count
FROM performed_action
GROUP BY action_id;
Which results in this dataset:
action_id | count
3 -------------- 2
31 ------------ 1
Relation in the User model:
public function getPerformedActions()
{
return $this->hasMany(PerformedAction::class, ['user_id' => 'id']);
}
performed_action table is filled with this data:
What i tried:
$users = \common\models\User::find()
->joinWith(['performedActions' => function ($query) {
$query->select('action_id, COUNT(*) as count')->groupBy('action_id');
}])
->asArray()->one();
$performedActions = $users['performedActions'];
But this returns:
array(0) {
}
How do i achieve the above dataset in Yii2?