dopii22884 2015-06-25 11:29
浏览 62
已采纳

Yii Framework Undefined Offset:0

I am trying to use CGridView with custom query, and trying to build a very simple with no sorting and stuff.

My View contains simple CGridView

 $this->widget('zii.widgets.grid.CGridView', array(
     'dataProvider'=>$dataProvider,
 ));

And my controller passes the $dataProvider to the view

 $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM ( ' . $query . ' ) as count')->queryScalar();
 $dataProvider=new CSqlDataProvider($query, array(
         'keyField' => false,
         'totalItemCount'=>$count,
         'pagination'=>array(
         'pageSize'=>10,
       ),
));

I don't have a keyField therefore I have set it to false. Moreover, I have tried printing out data using var_dump, data is present in the variable, but still I get this undefined offset error.

  • 写回答

2条回答 默认 最新

  • duanchu3376 2015-06-25 13:47
    关注

    You need to set the mapping for sorting.

    /*
    Query results
    array(
        array(
            'id' => 1,
            'username' => 'username',
            'email' => 'email'
        ),
        ...
    )
    */
    
    return new CSqlDataProvider($query, array(    
        'keyField' => 'id', //required, any field from query results
        'totalItemCount'=> $count,
        'pagination' => array(
            'pageSize' => 10
        ),
        'sort' => array(
            'defaultOrder' => array(
                'username' => CSort::SORT_DESC,            
            ),
            'attributes' => array(
                'username', 
                'email',            
            ),
        ),
    ));
    //grid.columns
    array(
        array(
            'name' => 'id' //WO sort
        ),
        array(
            'name' => 'username', //with sort (isset in dp.sort.attributes)      
        ),      
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器