I am having very difficult time to select usernames of all posts in the blog demo given in Yii..
author is relation of post class with user…
$criteria = new CDbCriteria;
$criteria->with='author';
$criteria->select='author.username';
$dataProvider=new CActiveDataProvider('Post', array(
'criteria' => $criteria,
));
var_dump($dataProvider->getData());
Error:
Active record “Post” is trying to select an invalid column “author.username”. Note, the column must exist in the table or be an expression with alias.
Now in your case what is happening is you’re trying to write some relation’s column in select, which will be there in select even without writing it, but as there is no corresponding variable to map this value yii is throwing an error..
So first thing if you need to username of auther in response, you can get it by relation calling, which won’t be a database call, and u dont need to write a select..
And if u want to call the username as a part of the post model only u have got to declare it as a property in model, and then specify alias in select..
Now it won’t throw error, and you can access the username by both ways..
$post->auther_username, and$post->auther->username