public function getList()
{
$users = $this->db->get('users')->result();
if ($users) {
foreach ($users as $user) {
$user->messages = $this->db->where('user_id',$user->id)->get('messages')->result();
}
}
return $users;
}
$users = Users::model()->findAll();
$user = Users::model()->findByPk($id);
class Users extends CActiveRecord
{
…
public function relations()
{
return array(
'messages'=>array(self::HAS_MANY, 'Messages', 'user_id')
);
}
}
$user->messages