Hi. I don’t unrestand how relate tables… I read the documentation, but I can’t do it…
I have 2 tables:
Users
id
name
UserInfo
id
id_user
address
This relation is ok? In the Users Model
‘user’ => array(self::HAS_MANY, ‘UserInfo’, ‘id_user’),
In the gridview:
array(
‘name’=>‘id_user’,
‘value’=>’$data->user->address’,
),
Why I can’t see the address in the gridview?
oligalma
(Marc Oliveras)
2
$data->user returns an array of UserInfo objects, so you should put $data->user[0]->address to grab the first element
you could also put type => ‘raw’
array(
'name'=>'id_user',
'type'=>'raw',
'value'=>'$data->user[0]->address',
),
Why a user can have more than one user-information? your case is not usual,
so I assume you just have one user-information for each user, then the code will be like this:
class Users extends CActiveRecord
{
...
public function relations()
{
return array(
'info' => array(self::HAS_ONE, 'UserInfo', 'user_id')
);
}
...
}
class UserInfo extends CActiveRecord {
...
public function relations()
{
return array(
'user' => array(self::BELONGS_TO, 'Users', 'id')
);
}
...
}
so in view, you can display address of user by this way:
array(
'name'=>'id_user',
'value'=>'$data->info->address',
),
or try this if you want:
array(
'name'=>'id_user',
'value'=>function($data){
return $data->info->address;
}
)