gpt4 book ai didi

php - 在 CGridView 中显示另一个模型的属性

转载 作者:可可西里 更新时间:2023-11-01 00:16:12 28 4
gpt4 key购买 nike

在 Yii 中,我正在做多模型。我的数据库是这样的

 +++++ Group ++++++
id
name

+++++ Member ++++++
id
group_id
firstname
lastname
membersince

在组 Controller 中,我想显示成员的属性。一切正常,但是当我使用菜单中的管理选项时,它显示两个模型的属性,但在两个不同的 GridView 中。我想显示两个模型的属性在单个 GridView 中。 Member Controller 的代码是这样的

  public function actionAdmin()
{
$model=new Group('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Group']))
{
$model->attributes=$_GET['Group'];
}
$member=new Member('search');
$member->unsetAttributes(); // clear any default values
if(isset($_GET['Member']))
{
$model->attributes=$_GET['Member'];
}
$this->render('admin',array(
'model'=>$model,
'member'=>$member,
));
}

在组管理代码中查看是这样的

 <?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'member-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'class'=>'CButtonColumn',
),
),
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'member-grid',
'dataProvider'=>$member->search(),
'filter'=>$member,
'columns'=>array(
'firstname',
'lastname',
array(
'class'=>'CButtonColumn',
),
),
));

这里我使用了两次 CGridView 来显示两个属性的模型。所以有人能告诉我如何在一个单一的CGridView.Any 帮助和建议将非常有用。[更新]模型中的关系:组模型

public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'member' => array(self::HAS_MANY, 'Member', 'group_id'),
);
}

成员(member)模型:

 public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'group' => array(self::BELONGS_TO, 'Group', 'group_id'),
);
}

最佳答案

在 yii 中访问相关模型字段的一种简单方法是使用类似这样的东西
$model->relatedModel->field -- 如果模型之间存在has_onebelongs_to 关系,则可以使用它。
因此,在您的情况下,您可以使用代码
访问成员的组名$memberModel->group->name
但是当您需要访问has_manyma​​ny_many 关系类型的相关模型字段时,您需要执行类似
$model->relatedModel[arrayIndex]->field
这是因为在这种情况下有很多相关模型,yii 会自动给你一个数组中的相关模型。
在您的情况下,组有很多成员,要访问组的特定成员(比如第一个成员,即 arrayIndex = 0),您可以使用 $groupModel ->成员[0]->名字
现在来回答您的确切问题,首先,您不需要声明或初始化或传递 $member 模型。所以你的 Controller Action 可以是

public function actionAdmin(){
$model=new Group('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Group'])){
$model->attributes=$_GET['Group'];
}
$this->render('admin',array(
'model'=>$model
)
);
}

那么显然在您看来您不需要这两个 GridView

<?php 
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'member-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array( // this is for your related group members of the current group
'name'=>'members.firstname', // this will access the attributeLabel from the member model class, and assign it to your column header
'value'=>'$data->members[0]->firstname', // this will access the current group's 1st member and give out the firstname of that member
'type'=>'raw' // this tells that the value type is raw and no formatting is to be applied to it
),
array(
'class'=>'CButtonColumn',
),
),
));

希望这对您有所帮助。

关于php - 在 CGridView 中显示另一个模型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117099/

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