2011-11-28 54 views
0

我想在模型的关系函数内做一个复杂的STAT。Yii的复数STAT GROUP BY

基本上是:

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

但Yii的不会允许的。所以我做了一个self :: HAS_ONE,但Yii在检索模型数据时在ENTIRE查询上进行了连接和分组。

我想要么得到STAT工作,所以它做一组由不同的数据,或得到的地方,原始SQL变得

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

效果是任何这可能吗?

+0

当您尝试时,您是否收到错误消息? –

+0

是的,我收到一条错误信息 – Inferno

+0

不管你做什么,都不要告诉我们它是什么。 –

回答

1

您需要使用两个STAT关系:

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

没有办法将它们组合成使用STAT之一。