Reputation: 1915
In my ClistView i'm trying to set a default sort and define my sortable attributes in my view. I've gotten this far
in my 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.
)
),
));
in my model
public function relations()
{
return array(
'_state' => array(self::BELONGS_TO, 'State', 'state'),
'_make' => array(self::BELONGS_TO, 'pMake', '',
'foreignKey' => array('make_code'=>'make_code')),
);
}
and in my view
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'sortableAttributes'=>array(
'_make' => 'Make',
'store',
'state',
),
));
im getting this error
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
how to i sort table pMake.make
?
Upvotes: 1
Views: 273
Reputation: 2619
try this in your 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"',
));
then
$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.
)
),
));
then in your view
'sortableAttributes'=>array(
'make' => 'Make', //you can call "make" base on 'attributes'=>array('make'=>array())
'store',
'state',
),
note tested. hope it works.
Upvotes: 1