У меня проблема с CGridView в Yii.
Если я использую Table в качестве модели, то CGridView работает нормально. Может быть отсортировано и отфильтровано.
Но если я использую View в качестве модели, CGridView не может быть отсортирован ..
Пожалуйста, помогите,
Спасибо
Ниже мой код
index.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'unit-history-grid',
'cssFile' => Yii::app()->request->baseUrl . '/css/gridview.css',
'dataProvider'=>$model->search(),
'filter'=>$model,
'enableSorting'=>false,
'columns'=>array(
'serial_no',
'customer_name',
'visit_count',
'startup_serviceman',
array(
'header' => 'Startup Date',
'name' => 'startup_date',
'type' => 'raw',
'value' => 'AppHelper::formatDate($data->startup_date)',
'filter' => false,
),
array(
'header' => '',
'type' => 'raw',
'value' => '"<a href=\"'. Yii::app()->request->baseUrl .'/inquiry/unitHistory/visit/serial_no/". $data->serial_no ."\">History Visit</a>"',
),
array(
'header' => '',
'type' => 'raw',
'value' => '"<a href=\"'. Yii::app()->request->baseUrl .'/inquiry/unitHistory/spareParts/serial_no/". $data->serial_no ."\">History Recommended Parts</a>"',
),
),
)); ?>
Моя модель: ViewUnitHistory.php
class ViewUnitHistory extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return ViewUnitHistory the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'view_unit_history';
}
public function primaryKey(){
return 'serial_no';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('serial_no, customer_name', 'required'),
array('serial_no', 'length', 'max'=>30),
array('customer_name', 'length', 'max'=>100),
array('visit_count', 'length', 'max'=>21),
array('startup_date, startup_serviceman', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('serial_no, customer_name, visit_count, startup_date, startup_serviceman', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'customerProduct' => array(self::BELONGS_TO, 'CustomerProduct', 'serial_no'),
'userCreate' => array(self::BELONGS_TO, 'User', 'created_by'),
'userModify' => array(self::BELONGS_TO, 'User', 'modified_by'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'serial_no' => 'Serial No',
'customer_name' => 'Customer Name',
'visit_count' => 'Visit Count',
'startup_date' => 'Startup Date',
'startup_serviceman' => 'Startup Serviceman',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('t.serial_no',$this->serial_no,true);
$criteria->compare('t.customer_name',$this->customer_name,true);
$criteria->compare('t.visit_count',$this->visit_count,true);
$criteria->compare('t.startup_date',$this->startup_date,true);
$criteria->compare('t.startup_serviceman',$this->startup_serviceman,true);
$user = User::model()->findByPk(Yii::app()->user->getState('user_id'));
if ($user->branch_id != NULL) {
$criteria->addCondition('a.branch_id = ' . $user->branch_id);
}
$criteria->with = array(
'customerProduct.customer' => array('alias'=>'a'),
);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'serial_no'=>array(
'asc'=>'t.serial_no',
'desc'=>'t.serial_no DESC',
),
'customer_name'=>array(
'asc'=>'t.customer_name',
'desc'=>'t.customer_name DESC'
),
),
),
));
}
}
Ниже приведена функция actionIndex в моем контроллере
/**
* Lists all models.
*/
public function actionIndex()
{
$model=new ViewUnitHistory('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['ViewUnitHistory']))
$model->attributes=$_GET['ViewUnitHistory'];
$this->render('index',array(
'model'=>$model,
));
}