具有beforeValidate()值的yii2 gridview过滤器

具有beforeValidate()值的yii2 gridview过滤器,gridview,yii2,Gridview,Yii2,我有一个由gii CRUD生成的yii2网格视图 我将浏览器指向/model/index。I不在GET字符串中包含任何搜索值。但是GridView过滤器预先填充了值。 即使我尝试删除或替换这些过滤器值,预填充的值也会弹回来 我注意到这些预先填充的值来自模型的beforeValidate()方法 public function beforeValidate() { if (parent::beforeValidate()) { if ($this->isNewRec

我有一个由gii CRUD生成的yii2网格视图

我将浏览器指向
/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()之前的
并将这些值用于筛选器

  • 为什么会这样
  • 我可以做些什么来获得过滤器值的干净记录
(在PHP7.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;
}

您正在
beforeValidate()
中设置属性,这些属性在调用
$this->validate()
时应用(无论验证结果如何)

你需要把这台电视机移到别的地方,或者

如果我猜对了,您确实在
设置了
created_,在
设置了
updated_,因为这些字段在您的数据库中被标记为非空,并且因为gii为它们生成了所需的
规则。
处理此类属性的正确方法是删除它们的
required
规则,并添加
yii\behaviors\TimestampBehavior
,负责在模型保存步骤中设置它们。
这样,您就不会看到模型的验证错误,也不必手动设置这些错误,数据库列也会正确填充


整个GridView问题都消失了。

基于解决方案,我们也可能不使用时间戳行为,只是我们应该取消所需的,然后在保存之前将该逻辑放入
。如果您想执行任何逻辑操作或创建一些预先设定的值,这是一种更通用的解决方案。

您可以显示您的搜索模型吗?是的,您猜对了。我将把它转换为
时间行为
。谢谢!