YII中的CArrayDataProvider排序不适用于某些字段

时间:2016-03-30 06:26:20

标签: php sorting yii

我在控制器中创建了Dataprovider并将其传递给视图文件。它显示正确的数据。总共有7列要显示。 但只有2个字段排序不起作用并抛出ERROR-500。

在控制器文件中,我的代码是:

    $OutboundRatecardDetails_data = OutboundRatecardDetails::model()->findAllByAttributes(array('orc_id'=>$orc_id));

    $pageSize = 10;
    if(isset($_GET['pageSize']))
    {
        $pageSize = $_GET['pageSize'];
        Yii::app()->user->setState('pageSize', (int)$_GET['pageSize']);
        unset($_GET['pageSize']);
    }

    $dataProvider=new CArrayDataProvider($OutboundRatecardDetails_data, array(
        'id'=>'orcd_id',
        'sort'=>array(
        'attributes'=>array(
            'orcd_id', 'des_id', 'orcd_duration','orcd_rate','orcd_grace_duration','orcd_description'
        ),
        ),
        'pagination'=>array(
        'pageSize'=>$pageSize,
        ),
        'keyField'=>'orcd_id',
    ));

    $this->render('index',array(
        'dataProvider'=>$dataProvider,
    ));

在admin.php视图文件中,我的代码是:

    <?php  $this->widget('application.components.widgets.tlbExcelView', array(
        'id'=>'Outbound-rate-card-details-grid',
        'dataProvider'  =>$dataProvider,
        'itemsCssClass' =>'table table-bordered table-striped table-condensed',
        'pagerCssClass' =>'dataTables_paginate paging_bootstrap pagination',
        'htmlOptions'   =>array('class'=>'adv-table'),        
        'columns'=>array(
            'des_id'=>array(
            'name'=>'des_id',
            'header'=>'Destination',                
            'value' => '(!empty($data->rel_destination->des_prefix) ? $data->rel_destination->des_prefix : "")',
            ),

            array(
            'name'=>'orcd_grace_duration',
            'header'=>'Grace Duration',
            'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
            ),

            array(
            'name'=>'orcd_duration',
            'header'=>'Incr. Duration',
            'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
            ),

            array(
            'name'=>'orcd_rate',
            'header'=>'Incr. Rate',
            'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
            ),

            array(
            'name'=>'orcd_min_duration',
            'header'=>'Min. Duration',
            'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
            ),

            array(
            'name'=>'orcd_min_rate',
            'header'=>'Min. Rate',
            'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
            ),

            array(
            'name'=>'orcd_description',
            'header'=>'Rate Description',
            'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
            ),
        ),
    )); ?>

当我尝试对第二和第三个字段(orcd_grace_duration,orcd_duration)进行排序时,它会抛出错误。还附加了错误屏幕截图。除了这两个领域,排序是完美的。

enter image description here

1 个答案:

答案 0 :(得分:1)

当CArrayDataProvider的列具有相同值时,会发生此问题。 请尝试使用CActiveDataPrider。

您只需更改控制器中的代码,如下所示:

    $criteria = new CDbCriteria;
    $criteria->compare('orc_id',$orc_id,true);

    $dataProvider = new CActiveDataProvider('OutboundRatecardDetails', array(
            'criteria'=>$criteria,
            'sort'         =>  array(
                'defaultOrder' =>  'orc_id desc',
            ),
            'pagination'=>array(
            'pageSize'=>$pageSize,
        ),
    ));