CListView的默认排序和可排序属性


default sort and sortableattributes for CListView

在我的ClistView中,我试图设置一个默认排序,并在视图中定义我的可排序属性。我已经完成了这么远的

在我的actionIndex()

$criteria=new CDbCriteria(array(                    
                            'condition'=>'make_code!="00"', 
                    ));
$dataProvider=new CActiveDataProvider('StoreNew', array(
                    'criteria'=>$criteria,
                    'sort'=>array(
                       'defaultOrder'=>'_make DESC',
                        'attributes'=>array(
                            '_make'=>array(
                                'asc'=>'_make',
                                'desc'=>'_make DESC',
                            ),
                          '*', //if attributes contains a star ('*') element, the name will also be used to match against all model attributes.
                        )
                    ),
        ));

在我的型号中

public function relations()
    {
        return array(
            '_state' => array(self::BELONGS_TO, 'State', 'state'),
            '_make' => array(self::BELONGS_TO, 'pMake', '', 
                            'foreignKey' => array('make_code'=>'make_code')),
        );
    }

在我看来

$this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'sortableAttributes'=>array(
        '_make' => 'Make',
        'store',
        'state',
    ),
));

我收到这个错误

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column '_make' in 'order clause'. The SQL statement executed was: SELECT * FROM `store_new` `t` WHERE make_code!="00" ORDER BY _make DESC LIMIT 10 

如何对pMake.make表进行排序?

在您的actionIndex() 中尝试此操作

$criteria=new CDbCriteria(array(                    
                                'with' => array('_make'),  // join the _make relation you defined in your model into this query
                                'condition'=>'t.make_code!="00"',
                        ));

然后

$dataProvider=new CActiveDataProvider('StoreNew', array(
                    'criteria'=>$criteria,
                    'sort'=>array(
                       'attributes'=>array(
                            'make'=>array(
                                'asc'=>'_make.make',
                                'desc'=>'_make.make DESC',
                            ),
                          '*', //if attributes contains a star ('*') element, the name will also be used to match against all model attributes.
                        )
                    ),
        ));

那么在你看来

'sortableAttributes'=>array(
    'make' => 'Make', //you can call "make" base on 'attributes'=>array('make'=>array())
    'store',
    'state',
),

笔记测试。希望它能起作用。