2011-11-28 41 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之一。