Inferno
Inferno

Reputation: 1

Complex STAT GROUP BY for Yii

I'm trying to do a complex STAT within the relation function of the model.

Basically:

'stats' => array(self::STAT, 'Comments', 'post_id', 'select'=>'COUNT(id) AS total_comments, SUM(comment_rating) AS comment_rating')

But Yii won't allow that. So I've done a self::HAS_ONE but Yii does a join and group by on the ENTIRE query upon retrieval of model data.

I want to either get the STAT working so it does one group by for different data, OR get it to the effect where the raw SQL becomes

SELECT * FROM Posts p LEFT JOIN (SELECT .....) AS comments_data CD ON p.id=CD.post_id

Is any of this possible?

Upvotes: 0

Views: 1788

Answers (1)

Greg Satir
Greg Satir

Reputation: 534

You need to use two STAT relations:

'commentCount'=>array(self::STAT, 'Comment', 'post_id'),
'ratingSum'=>array(self::STAT, 'Comment', 'post_id', 'select'=>'SUM(comment_rating)'),

There is no way to combine them into one using STAT.

Upvotes: 1

Related Questions