gpt4 book ai didi

php - 在 yii 中需要帮助以在 CGridView 中显示两个表数据?

转载 作者:搜寻专家 更新时间:2023-10-31 20:40:15 25 4
gpt4 key购买 nike

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

表格信息-

分支机构

Id
branch_name

用户

Id
branch_id
user_name

关系-

分支主管

public function relations()
{
return array(
'users' => array(self::HAS_MANY, 'User', 'branch_id'),
);
}

用户管理员

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

现在工作正常!!

关于php - 在 yii 中需要帮助以在 CGridView 中显示两个表数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23823160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com