CListView的默认排序和sortableattributes

时间:2014-09-27 09:47:42

标签: php yii

在我的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进行排序?

1 个答案:

答案 0 :(得分:1)

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',
),
注意测试。希望它有效。