dqmq0654 2014-03-31 01:47
浏览 43
已采纳

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

In the Type model's search() function, I want to display three attributes in CGridview:

type.name, type.description and 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,
    ));
}

This is the view:

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

but in this way the columns in CGridView can only display model attributes, it shows type.number can't find in model Type, how can I display the count()?

  • 写回答

1条回答 默认 最新

  • dtwzwmv87399 2014-03-31 03:17
    关注

    You will also have to add the attribute to your model to be able to use it as a column in your CGridView:

    class Type extends CActiveRecord {
    
        public $number;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝