在Yii中对CSqlDataProvider进行排序

时间:2013-07-03 08:50:46

标签: php sorting yii csqldataprovider

我有一个相当复杂的查询,我使用CSqlDataProvider:

        $sql = "
          SELECT  c.id AS id,
                  c.enabled AS enabled,
                  s.store_name AS store_name,
                  s.store_url AS store_url,
                  c.verified AS verified,
                  r.rating_total AS rating
          FROM compete_settings c
          LEFT JOIN stores s
            ON c.compete_id = s.id
          LEFT JOIN (
            (SELECT store_id, rating_total FROM amazon_sellers)
            UNION
            (SELECT store_id, rating_total FROM ebay_sellers)
          ) AS r
            ON c.compete_id = r.store_id
          WHERE c.store_id = :store_id
        ";
        $itemCount = Yii::app()->db->createCommand("SELECT COUNT(id) FROM compete_settings WHERE store_id = $store_id")->queryScalar();
        return new CSqlDataProvider($sql, array(
            'totalItemCount' => $itemCount,
            'params' => array(
                'store_id' => $store_id,
            ),
            'sort' => array(
                'attributes' => array ( 'enabled, store_name, rating' ),
                'defaultOrder' => array('store_name'=>false)
            ),
            'pagination' => array('pageSize' => Yii::app()->user->getState('pageSize_cs', Yii::app()->params['defaultPageSize']),),
        ));

查询效果很好。但是,列不可排序。

我确保关注Yii CGridview sorting with CSqlDataProvider,但它们仍然不是可点击的列标题。

以下是我的列数组:

'columns' => array(
    array(
        'header' => 'Enabled',
        'type'=>'raw',
        'name'=>'enabled',
        'htmlOptions'=>array('class'=>'enabled-column'),
        'value' => 'CHtml::checkbox("", $data["enabled"], array("id"=>"c{$data[\'id\']}", "class"=>"enable-switch"))',
    ),
    array(
        'header' => 'Name',
        'htmlOptions' => array('class' => 'account-name-column'),
        'type' => 'raw',
        'name'=>'store_name',
    ),
    array(
        'header' => 'Store Rating',
        'name'=>'rating',
    ),
),

帮助? ):

1 个答案:

答案 0 :(得分:2)

与我在后面提到的问题中提到的不同,Yii接受attributes param作为值数组而不是具有一个CSV值的数组。

所以:

'sort' => array(
    'attributes' => array ( 'enabled', 'store_name', 'rating' ),
    'defaultOrder' => array('store_name'=>false)
),

而不是:

'sort' => array(
    'attributes' => array ( 'enabled, store_name, rating' ),
    'defaultOrder' => array('store_name'=>false)
),