Yii CGridView无法显示自定义SQL属性

时间:2014-03-31 01:47:37

标签: php yii

Type模型的search()函数中,我想在CGridview中显示三个属性:

type.nametype.descriptioncount(dataset_type.dataset_id)

public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;
            $criteria->alias='t';
            $criteria->select='t.name, t.description, count(dataset_type.dataset_id) as number';
            $criteria->join='LEFT JOIN dataset_type ON dataset_type.type_id=t.id';
            $criteria->group='t.id';


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

这是观点:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'type-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'itemsCssClass' => 'table table-bordered',
    'columns' => array(
        'name',
        'description',
        'number',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

但是通过这种方式,CGridView中的列只能显示模型属性,它显示type.number无法在模型Type中找到,如何显示count()?< / p>

1 个答案:

答案 0 :(得分:1)

您还必须将属性添加到模型中才能将其用作CGridView中的列:

class Type extends CActiveRecord {

    public $number;