微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在Yii我正在做多模型.我的数据库是这样的

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

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

在Group控制器中,我想显示Member的属性.一切正常,但是当我从菜单中使用manage选项时,它会显示两个模型的属性,但是在两个不同的grid-view中.我想在一个单元中显示两个模型属性网格视图.
 Member控制器的代码是这样的

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

会员型号:

 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-&GT&组 – GT;名
但是当您需要访问has_many或many_many关系类型的相关模型字段时,您需要执行类似的操作
$模型 – &GT relatedModel [arrayIndex] – GT字段
这是因为在这种情况下有许多相关模型,yii会自动为您提供数组中的相关模型.
在你的情况下,一个组有许多成员并且访问一个组的特定成员(比如第一个成员,即arrayIndex = 0)你可以使用$groupModel-> members [0] – > firstname
现在回答您的确切问题,首先,您不需要声明或初始化或传递$member模型.所以你的控制器动作可以

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

那么显然在你的视图中你不需要两个网格视图

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

希望这可以帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐