I have these classes:
class User extends CActiveRecord
{
...
public function relations()
{
return array(
'user_details' => array(self::HAS_MANY, 'UserDetails', 'user_id'),
'users_applications' => array(self::HAS_MANY, 'UsersApplications', 'user_id'),
);
}
}
...
class UserDetails extends CActiveRecord
{
...
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'division' => array(self::BELONGS_TO, 'Division', 'division_id'),
'department' => array(self::BELONGS_TO, 'Department', 'department_id'),
);
}
}
...
class Division extends CActiveRecord
{
...
public function relations()
{
return array(
'departments' => array(self::HAS_MANY, 'Department', 'division_id'),
'user_details' => array(self::HAS_MANY, 'UserDetails', 'division_id'),
);
}
}
I create a CDbCriteria object, which I will use to find all User objects with their relations. How do I write the select property if I want to select specific columns from the UserDetails and Division objects?
$criteria = new CDbCriteria();
$criteria->with = array('user_details', 'user_details.division', 'user_details.department');
$criteria->select = array(
't.id',
't.lastname',
'user_details.telephone',
'division.name',
);
This sample code throws an exception, saying that it cannot find user_details.telephone. One change regarding AR in 1.1.0 is that relational tables are now prefixed with the name of the relation. So, why doesn’t this code work? What should I write in order to select the relation of the relation of User (for example the department or division of a user)?
I’m sure I’m missing something here…