2015-03-03 116 views
2

我想在cGridview使用過濾器與cActiveDataProvider但它提供了以下異常的Yii:在網格視圖CActiveDataProvider篩選提供了錯誤

CActiveDataProvider其行爲沒有方法或關閉名爲「getValidators」

請檢查下面的代碼

控制器

public function actionAdmin() 
{ 
    $admin_type=Yii::app()->user->isAdmin; 
    if($admin_type==1) 
    { 
     $admin_batches=WebHelper::getAllAdminBatchesInArray(Yii::app()->user->getId()); 
     if(!empty($admin_batches)) 
     { 
      $batch_list=implode(",",$admin_batches); 
     } 
     $batch_criteria="group_id IN (".$batch_list.") and status!=-1"; 
    } 
    else 
     $batch_criteria="1 and status!=-1"; 

    $model=new CActiveDataProvider('Users',array('criteria'=>array(
       'condition'=>$batch_criteria, 
      ))); 
    // print_r($model);die; 

    //$model->unsetAttributes(); // clear any default values 
    if(isset($_GET['Users'])) 
     $model->attributes=$_GET['Users']; 

    $this->render('admin',array(
     'model'=>$model, 
    )); 
} 

查看

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid', 
'dataProvider'=>$model, 
'filter'=>$model, 
'columns'=>array(
    array(
     'class'=>'CButtonColumn', 
     'htmlOptions'=>array('width'=>'70px'), 
     'buttons' => array(
          'view' => array(
           'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/view-image.png' 
          ), 
          'update' => array(
           'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/Edit-icon.png' 
          ), 
          'delete' => array(
           'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/Delete-icon.png' 
          ) 
         ) 
    ), 
    first_name', 
    'last_name', 
    'user_name', 
    'group.group_title', 
    //'password', 
    'email_id', 
    array(
     'type'=>'raw', 
     'name'=>'Status', 
     'value'=>'($data->status==0)?"<a href=\'\' id=\'$data->user_id\' class=\'user_status Active\'>Active</a>":"<a href=\'\' id=\'$data->user_id\' class=\'user_status Inactive\'>Inactive</a>"' 
    ), 



),));?> 

回答

1

我產生一個新的警予項目,並在我的數據庫做了company表,然後我產生了GII用於測試的CRUD。這是在每個生成的YII控制器actionAdmin默認結構:

public function actionAdmin() { 
    $model = new Company('search'); 
    $model->unsetAttributes(); // clear any default values 
    if (isset($_POST['Company'])) 
    $model->attributes = $_POST['Company']; 

    $this->render('admin', array(
     'model' => $model, 
    )); 
} 

而且在針對每個生成CRUD默認admin視圖,YII傳遞$model到CGridView filter屬性。 這意味着,CGridView的過濾器只接受模型的對象,但您將CActiveDataProvider的實例傳遞給它。假設$model是模型對象(例如,您的案例中的用戶),您應該將$model傳遞給CGridView的filter,並將$model->search()傳遞給dataProvider of CGridView。然後,您可以在$model->search()方法中構建您的標準。我建議你在yii中生成一個示例CRUD並查看生成的代碼。

+0

@ talhamalik22:你試過我的解決方案嗎? – hamed 2015-03-03 15:58:39