2012-03-08 29 views
2

Yii中的CGridView存在問題。Yii:使用數據庫視圖作爲模型時無法對CGridView列進行排序

如果我使用Table作爲模型,那麼CGridView工作正常。它可以被分類和過濾。

但是,如果使用查看型號,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, 
     )); 
    } 
+0

作爲一種模式意味着什麼? – 2012-03-08 20:26:21

+0

我的意思是我使用數據庫視圖,而不是表格作爲Yii中的模型(CActiveRecord)。 – 2012-03-09 04:51:13

+0

你正在寫的一些代碼可能有幫助 – 2012-03-09 13:28:57

回答

1

哥哥,我可以看到控制器?基本上我認爲問題在於你在模型中使用了這個視圖。但是用事實表或數據庫視圖替換它。現在在Db視圖中沒有出現所有列,您在CGRidView中指向的內容

+0

嗨Afnan,我已經添加了我的控制器代碼的一部分。 – 2012-03-13 02:25:26

+0

你是通過點擊'CGrid' – 2012-03-13 15:43:01

+0

yups的頭部來排序的,這就是我的意思。標題不能被點擊,我不知道爲什麼 – 2012-03-14 01:44:13

0

我剛測試過這個,它工作得很好。我能夠基於視圖(我必須定義模型的主鍵但其餘部分變得很好)使用Gii full crud生成。

現在我來看看'enableSorting'=> false是什麼,那可能是:)。

1

說實話我並不真正明白惠普排序的問題在哪裏。我只是要一點在CGridView部件「enableSorting」明顯

=>假

,而該屬性設置爲false網格是不可排序。如果在生成網格時無法自動排序,我認爲它不會因爲無論如何不按照你想要的方式排序而傷害它。

這樣做只是做

$criteria->with = array(
     'customerProduct.customer' => array('alias'=>'a'), 
    ); 

$criteria->order = 't.serial_no DESC'; 

在CActiveDataProvider排序陣列嘗試進行排序它具有「enableSorting」屬性設置爲false,那麼ofcourse它不能做到這一點的網格。

如果你想排序網格只是啓用排序,如果你沒有做標準的順序,它會按照你想要的順序返回按db排序的結果。

希望這有助於

相關問題