2

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_name而不last_name定义模型中的关系?

4

2 回答 2

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

),
于 2013-06-11T03:38:46.133 回答
1

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

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

在您的 Grid 中调用它。

array('name'=>'name' or 'header'=>'some header','value'=>'$data->fname')
于 2013-06-10T12:31:02.850 回答