문제

I build a website application using Yii Framework.

I want to use model with query like this:

SELECT u.id, u.username, u.score, (SELECT COUNT(ownerId) FROM post p WHERE p.ownerId = u.id) AS totalPost 
FROM users u 
ORDER BY u.score DESC, totalPost DESC LIMIT 10

Please help me to convert the query into models in Yii Framework.

도움이 되었습니까?

해결책

Here is related chapter from "The Definite Guide To Yii" - http://www.yiiframework.com/doc/guide/database.arr

다른 팁

Well I think you can simplify this by using a relation 'TotalPost' => array(self::STAT,'Post','OwnerId')

Check CStatRelation for more details. Note that in this class, the select property is the statistical expression which is by default COUNT(*).

Also, your query can be mapped into User model as long as you have a public $TotalPost; in model class User.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top