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

2022-01-04 00:00:00 php yii gridview

在 Yii 中,我在做 multimodel.我的数据库是这样的

In Yii I am doing multimodel.My database is something like this

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

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

在组控制器中,我想显示成员的属性.一切正常,但是当我使用菜单中的管理选项时,它显示两个模型的属性,但在两个不同的网格视图中.我想显示两个模型的属性在单个网格视图中.成员控制器的代码是这样的

In Group controller I want to show Member's attributes.Everything is working fine but when I am using manage option from the menu it is showing the attributes for both models but in two different grid-view.I want to show both models attributes in a single grid-view. The code for Member controller is like this

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

for View in Group admin 代码是这样的

for View in Group admin code is like this

 <?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 帮助和建议将是非常有用的.[更新]模型中的关系:组模型

Here I have used CGridView for two times to show models for both attributes. So can someone tell me how to show models in a single CGridView.Any help and suggestions will be highly appriciable. [Updated] Relations in Models: Group Model

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

会员型号:

 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_one 或 belongs_to 关系,则可以使用该字段.
因此,在您的情况下,您可以使用代码访问成员的组名
$memberModel->group->name
但是当您需要访问has_many 或ma​​ny_many 关系类型的相关模型字段时,您需要执行类似
$model->relatedModel[arrayIndex]->field
这是因为这种情况下有很多相关的模型,yii 会自动给你数组中的相关模型.
在您的情况下,一个组有许多成员,并且要访问一个组的特定成员(比如第一个成员,即 arrayIndex = 0),您可以使用 $groupModel->members[0]->firstname
现在来回答您的确切问题,首先,您不需要声明或初始化或传递 $member 模型.所以你的控制器动作可以是

A simple way to access related model fields in yii is to use something like this
$model->relatedModel->field -- this can be used if there is a has_one, or belongs_to relation between the models.
So in your case, you can access the group name of a member using the code
$memberModel->group->name
But when you need to access related model fields for has_many, or many_many relation types, you will need to do something like
$model->relatedModel[arrayIndex]->field
This is because there are many related models in this case, and yii automatically gives you the related model in an array.
In your case a group has many members and to access a particular member(say the first member, i.e arrayIndex = 0) of a group you can use $groupModel->members[0]->firstname
Now to coming to your exact question, first of all, you do not need to declare or initialize or pass the $member model. So your controller action can be

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
     )
  );
}

然后显然在您看来您不需要两个网格视图

Then obviously in your view you don't need the two grid-views

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

希望这会有所帮助.

相关文章