doushuhuai7247 2012-02-02 17:38
浏览 27
已采纳

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

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

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'),
    );
  }
  • 写回答

1条回答 默认 最新

  • dongye1143 2012-02-04 05:04
    关注

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

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题