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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么