簡體   English   中英

在yii中需要幫助才能在CGridView中顯示兩個表數據?

[英]Need help in yii to display two tables data in CGridView?

在yii中需要幫助才能在CGridView中顯示兩個表數據 -

表格信息 -

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_ONE而不是HAS_MANY

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM