Question
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.
Solution
Here is related chapter from "The Definite Guide To Yii" - http://www.yiiframework.com/doc/guide/database.arr
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow