显示CGridView中另一个模型的属性 [英] Showing attributes of another model in CGridView

查看:111
本文介绍了显示CGridView中另一个模型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在Yii中,我在做多模式。 ++
id
名称

+++++会员++++++
id
group_id
名字
姓氏
membersince

+++++ 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 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中显示模型。任何帮助和建议都会非常有价值。
[已更新]
模型中的关系:
组模型

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

会员模式:

Member 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(
      'group' => array(self::BELONGS_TO, 'Group', 'group_id'),
    );
  }


推荐答案

访问相关的简单方法yii中的模型字段是使用像这样的元素:
$ model-> relatedModel->字段 - 如果存在模型之间的 has_one belongs_to 关系。
因此,你可以使用你的情况来访问成员的组名称
$ memberModel-> group-> name

但是当你需要访问相关的模型字段对于 has_many many_many 关系类型,您需要执行如下操作:像
$ model-> relatedModel [ arrayIndex] - > field

这是因为在这种情况下有许多相关模型,而yii会自动给你数组中的相关模型。 b $ b在你的情况下,一个组有许多成员并访问gro的特定成员(比如说,第一个成员,即 arrayIndex = 0 )你可以使用 $ groupModel->成员[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',
         ),
      ),
   ));

希望这有助于您。

这篇关于显示CGridView中另一个模型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆