我有一個yii2 GridView,由gii CRUD生成。yii2帶有beforeValidate()值的gridview過濾器
我把我的瀏覽器指向/model/index
。 I 不要在GET字符串中包含任何搜索值。但是GridView過濾器預先填充了值。
即使我嘗試刪除或更換這些過濾器值,預填充值彈回。
我注意到這些預填充值來自模型的beforeValidate()
方法。
public function beforeValidate()
{
if (parent::beforeValidate()) {
if ($this->isNewRecord) {
$this->created_at = time();
$this->b_soft_deleted = 0;
}
$this->updated_at = time();
return true;
}
return false;
}
顯然,在GridView過濾呼叫beforeValidate()
和使用這些值過濾器...
- 這究竟是爲什麼?
- 我能做些什麼來獲得一個乾淨的篩選值?
更新(在PHP 7.0.14運行yii2 2.0.10)我的搜索模式是
<?php
namespace common\models\generated;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Article;
/**
* ArticleSearch represents the model behind the search form about `common\models\Article`.
*/
class ArticleSearch extends Article
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'title', 'description', 'positive_keywords', 'negative_keywords'], 'safe'],
[['created_at', 'updated_at', 'b_soft_deleted'], 'integer'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Article::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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([
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'b_soft_deleted' => $this->b_soft_deleted,
]);
$query->andFilterWhere(['like', 'id', $this->id])
->andFilterWhere(['like', 'title', $this->title])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'positive_keywords', $this->positive_keywords])
->andFilterWhere(['like', 'negative_keywords', $this->negative_keywords]);
return $dataProvider;
}
}
你能顯示您的搜索模式? – Bizley