在Type
模型的search()
函数中,我想在CGridview中显示三个属性:
type.name
,type.description
和count(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>
答案 0 :(得分:1)
您还必须将属性添加到模型中才能将其用作CGridView中的列:
class Type extends CActiveRecord {
public $number;