【问题标题】:Yii CGridView column names from two tables using idsYii CGridView 使用 ids 的两个表中的列名
【发布时间】:2018-04-16 09:21:10
【问题描述】:

index.php

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
        array(
            'header' => 'User Name',
            'value' => 'CHtml::encode($data->sud_profile->user_id)',//This will use the relationship and get all the details of the paticular user from users table
        ),
        'subject',
        'message',

我有两个表我想显示名称而不是 ID。
我的两张表是:

表 1

msg table
---------
id sender receiver subject message
------------------------------------
1   3      4         wish   have a god

表 2

--------
sud_profile
-------------
id user_id firstname lastname
1    2     kumar         rai

从这两个表中,我想根据 id 显示名称,例如 sender 和 user_id。

【问题讨论】:

  • 添加您的两个模型或共享您在模型中声明的关系以及msg 表中的senderreceiver 持有来自user 表的ID?你想显示name 吗?或 username 对于这些列,因为 sud_profile 表已经包含用户 firstlast 名称?
  • 嘿,如果对您有帮助,请务必将答案标记为正确

标签: view yii grid yii1.x


【解决方案1】:

您应该已经为各个模型添加了代码,或者在两个模型中添加了各自的关系,但我会尝试指导您,以便您可以根据您的代码跟进。

您可以调用Message模型内部定义的关系来访问用户,然后显示username

关于msg

我假设senderreceiver 列包含user 表中的id,您应该在Message 模型中定义关系,或者现在定义一个并将以下内容添加到您的关系数组(如果不同,请更改模型名称和字段名称)。

因为你有 senderreceiver 2 列都持有不同的 user_id 所以你需要定义 2 个关系。

 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(
          '_sender' => array(self::BELONGS_TO, 'User', 'sender'),
          '_receiver' => array(self::BELONGS_TO, 'User', 'receiver'),
        );
    }

然后在GridView 中将您的列配置更改为以下内容。

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
    array(
        'name'=>'sender',
        'header'=>'Sender',
        'htmlOptions'=>array('data-title'=>'Sender'),
        'value'=>'$data->_sender->username'
    ),
    array(
        'name'=>'receiver',
        'header'=>'Receiver',
        'htmlOptions'=>array('data-title'=>'Receiver'),
        'value'=>'$data->_receiver->username'
    ),

))
);

关于sud_profile

您需要对sud_profile 表执行相同的操作,定义关系并使用gridview 内的关系显示它。

您的关系如下所示

 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(
          '_user' => array(self::BELONGS_TO, 'User', 'user_id'),
        );
    }

并在您的 gridview 列配置中使用如下关系

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider' => $dataProvider,
    'columns' => array(
         array(
            'name'=>'user_id',
            'header'=>'Username',
            'htmlOptions'=>array('data-title'=>'Sender'),
            'value'=>'$data->_user->username'
         ),    
    ))
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多