2017-03-11 65 views
0

我有這個問題:我需要從數據庫中獲取數據並過濾它們。但後來我需要使用自定義php函數來過濾使用它的數據過濾結果。在ActiveDataProvider如何使用自定義php函數來過濾ActiveDataProvider

public function search($params) { 

    $query = Passenger::find(); 

    // add conditions that should always apply here 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 

    // I guess my function would go like here 
    Passenger::filterResultsEvenMore($dataProvider); 

    $this->load($params); 

    if (!$this->validate()) { 
     // uncomment the following line if you do not want to return any records when validation fails 
     // $query->where('0=1'); 
     return $dataProvider; 
    } 

    // grid filtering conditions 
    $query->andFilterWhere([ 
     'passenger_id' => $this->passenger_id, 
     // ... 
     'version' => $this->version, 
     'status' => $this->status, 
    ]); 

    return $dataProvider; 
} 

CLASIC搜索功能,所以我的問題是如何與數據提供程序的結果工作,因爲如果我vardump變量它看起來像這樣,有沒有實際的數據。

yii\data\ActiveDataProvider Object 
(
    [query] => common\models\PassengerQuery Object 
     (
      [sql] => 
      [on] => 
      [joinWith] => 
      [select] => 
      [selectOption] => 
      [distinct] => 
      [from] => 
      [groupBy] => 
      [join] => 
      [having] => 
      [union] => 
      [params] => Array() 
      [_events:yii\base\Component:private] => Array() 
      [_behaviors:yii\base\Component:private] => Array() 
      [where] => Array 
       (
        [status] => 1 
       ) 
      [limit] => 
      [offset] => 
      [orderBy] => 
      [indexBy] => 
      [emulateExecution] => 
      [modelClass] => common\models\Passenger 
      [with] => 
      [asArray] => 
      [multiple] => 
      [primaryModel] => 
      [link] => 
      [via] => 
      [inverseOf] => 
     ) 
    [key] => 
    [db] => 
    [id] => 
    [_sort:yii\data\BaseDataProvider:private] => 
    [_pagination:yii\data\BaseDataProvider:private] => 
    [_keys:yii\data\BaseDataProvider:private] => 
    [_models:yii\data\BaseDataProvider:private] => 
    [_totalCount:yii\data\BaseDataProvider:private] => 
    [_events:yii\base\Component:private] => Array() 
    [_behaviors:yii\base\Component:private] => 
) 

UPDATE

我需要使用功能,這樣每個記錄:

if (myFunction(table_column_1, table_column_2)) { 
    result_is_ok_return_it 
} else { 
    do_not_return_this_record 
} 
+1

使用'$ dataProvider-> models'得到結果。 –

+0

是不是很容易?我無法相信,我猜想,我猜想,如果你把它寫爲回覆,我會評價它 – Ripper

回答

1

你爲什麼不添加額外的過濾器來查詢的DataProvider使用的對象? 您可以將您的條件解析爲$query->andFilterWhere()。如果您需要自定義功能,只需修改$dataProvider->query對象內部函數。執行中的數據提供查詢後,你只能通過搜索結果進行過濾手動過濾存儲在$dataProvider->models

+0

我在'return $ dataProvider之前想';我會做foreach()和未設置的模型,但如果是隻讀的,它不可能。但是當我需要使用一些不能通過sql完成的自定義php函數時,如何在$ query-> andFilterWhere()中過濾這些函數? – Ripper

1

爲了得到結果使用模式陣列models propertygetModels()

例如,

$dataProvider->models; 

OR

$dataProvider->getModels(); 
0

我想我遇到了一個解決方案,(看起來像它在工作)

http://www.yiiframework.com/doc-2.0/yii-data-basedataprovider.html#setModels()-detail

我盡我通常搜索的東西后,如在一開始所描述的問題,我會用setModels()功能做這樣的事情

// ... 

    $filtered_models = []; 

    foreach ($dataProvider->models as $model) { 
     if ($model->status == 1) // example 
      $filtered_models[] = $model; 
    } 

    $dataProvider->setModels($filtered_models); 

    return $dataProvider;