需要帮助在yii显示两个表的数据在CGridView


Need help in yii to display two tables data in CGridView?

需要帮助在yii显示两个表的数据在CGridView -

Tables info -

<<p> 分支/strong>
Id
branch_name
用户

Id
branch_id
user_name
——

关系

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 />

我得到的记录很好,但user_name总是空值显示。

HAS_MANY改为HAS_ONE

'users' => array(self::HAS_ONE, 'User', 'branch_id'),

现在工作正常!!

没有得到值,因为$data部分是错误的-

$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
    ),
));