Reputation: 3628
Need help in yii to display two tables data in CGridView -
Tables info -
Branch
Id
branch_name
User
Id
branch_id
user_name
Relations -
BranchMaster
public function relations()
{
return array(
'users' => array(self::HAS_MANY, 'User', 'branch_id'),
);
}
UserMaster
public function relations()
{
return array(
'branchs' => array(self::HAS_MANY, 'Branch', 'Id'),
);
}
view.php
$this->widget('zii.widgets.grid.CGridView',
array(
'id'=>'my-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'columns'=>array(
'Id',
'branch_name',
array('name'=>'users.user_name', 'value'=>$data->User>user_name),
),
));
_view.php
<b><?php echo CHtml::encode($data->getAttributeLabel('Id')); ?>:</b>
<?php echo CHtml::encode($data->Id); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('branch_name')); ?>:</b>
<?php echo CHtml::encode($data->branch_name); ?>
<br />
<b><?php echo CHtml::encode($data->getAttributeLabel('user_name')); ?>:</b>
<?php echo CHtml::encode($data->user_name); ?>
<br />
I'm getting records fine but user_name always blank value display. Help me to solve my issue...
Upvotes: 4
Views: 516
Reputation: 219
You are not getting the value because the $data part is wrong -
$this->widget('zii.widgets.grid.CGridView',
array(
'id'=>'my-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'columns'=>array(
'Id',
'branch_name',
array(
'name'=>'users.user_name',
'value'=>'$data->users->user_name'), //here you used $data->User and need ' ' too
),
));
Upvotes: 1
Reputation: 3628
Just change the relation HAS_ONE
instead of HAS_MANY
'users' => array(self::HAS_ONE, 'User', 'branch_id'),
Working fine now!!
Upvotes: 2