在CGridView中显示连接表中的列

时间:2013-06-10 11:08:35

标签: php yii dataprovider

我通过CDbCriteria在模型中加入了一些表格,我的代码是这样的:

$crt = new CDbCriteria();
        $crt->alias = 'so';
        $crt->select = 'u.id, u.first_name, u.last_name';
        $crt->join = " inner join " . Flow::model()->tableName() . " as fl on fl.id = so.flow_id";
        $crt->join .= " inner join " . RoleUser::model()->tableName() . " as ru on ru.id = fl.receiver_role_user_id";
        $crt->join .= " inner join " . User::model()->tableName() . " as u on ru.user_id= u.id";
        $crt->compare('sms_outbox_group_id', $smsOutboxGroupId);
        $crt->compare('fl.kind', Flow::KIND_SMS);
        $crt->group = 'u.id';

        $smsOutBox = new SmsOutbox();
        return new CActiveDataProvider($smsOutBox, array(
            'criteria' => $crt,
            'sort' => array(
                'defaultOrder' => 'so.id DESC',
            )
        ));

如何在CGridView中显示我选择的列?有没有可能的方法来显示first_namelast_name而不在模型中定义关系?

2 个答案:

答案 0 :(得分:7)

我找到了使用Sudhanshu Saxena答案的解决方案。除了使用first_namelast_name的别名之外,我还为模型添加了两个属性,其名称与别名相同:receiverFirstNamereceiverLastName。通过这种方式我的问题得到了解决,此外它还提供了这两个属性的搜索功能。 我的最终代码是这样的:

Model :
public $receiverFirstName;
public $receiverLastName;

创建标准:

$crt = new CDbCriteria();
        $crt->alias = 'so';
        $crt->select = 'so.id,u.id as userId, u.first_name as receiverFirstName, u.last_name as receiverLastName, so.status';
        $crt->join = " inner join " . Flow::model()->tableName() . " as fl on fl.id = so.flow_id";
        $crt->join .= " inner join " . RoleUser::model()->tableName() . " as ru on ru.id = fl.receiver_role_user_id";
        $crt->join .= " inner join " . User::model()->tableName() . " as u on ru.user_id= u.id";
        $crt->compare('sms_outbox_group_id', $smsOutboxGroupId);
        $crt->compare('u.first_name', $this->receiverFirstName, true);
        $crt->compare('u.last_name', $this->receiverLastName, true);
        $crt->compare('so.status', $this->status);
        $crt->compare('fl.kind', Flow::KIND_SMS);
        $crt->group = 'userId';
        $crt->order = 'so.id';


        return new CActiveDataProvider($this, array(
            'criteria' => $crt,
        ));

最后在CgridView我这样做了:

'columns' => array(
    array(
        'header' => Yii::t('app', 'Row'),
        'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
    ),
    array(
        'name' => 'receiverFirstName',
        'value' => '$data->receiverFirstName',
    ),
    array(
        'name' => 'receiverLastName',
        'value' => '$data->receiverLastName',
    ),
    array(
        'name' => 'status',
        'value' => 'SmsOutbox::getStatusTitle($data->status)',
        'filter' => CHtml::listData(SmsOutbox::getStatusList(), 'id', 'title')
    ),

),

答案 1 :(得分:1)

使用别名并在CgridView中像这样调用

$crt->select = 'u.id, u.first_name as fname, u.last_name as lastname';

在你的网格中调用它。

array('name'=>'name' or 'header'=>'some header','value'=>'$data->fname')