In a sample blog application, I need the user details and number of posts by him. For that I used the following criteria:
$criteria->select='t.`id`, t.`name`, count(t1.`post_id`) AS `post_count`';
$criteria->join='JOIN {{posts}} `t1` ON t.`id` = t1.`user_id`';
$criteria->order='t.`name` ASC';
I used CActiveDataProvider in User model with the above criteria. When I executed the script, I got the error: "Property "User.post_count" is not defined.".
I tried a var_dump for $dataProvider->getData() and could see only the column that exists in the user table and not the column that I added in the select clause.
Check the generated SQL. To do so, configure a CWebLogroute in main.php, enable YII_DEBUG and don’t use yiit.php. You’ll see the SQL on the end of your page. That should help you to find out, what’s wrong.
SELECT t.`id`, t.`name`, count(t1.`post_id`) AS `post_count` FROM
`ss_users` `t` JOIN ss_posts `t1` ON t.`id` = t1.`user_id`
ORDER BY t.`name` ASC LIMIT 10
As you can see, the SQL query has the post_count column.
To check whether there is any problem in mysql(just in case…), i executed the query in mysql and i got all the three columns.