Yii2 gii生成的ModelSearch.php添加条件

我们在使用的gii生成的模型时,有时可能我们会需要用到一些追加条件不如 id不在某个数组中的信息此时我们可以修改

MedelSearch.php文件

例如为了不影响本来的搜索条件,我在类中新加一个方法

public function search2($params)
    {
        $query = comic::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([
            'id' => $this->id,
        ]);

        $query->andFilterWhere(['not in', 'id', $params]);

        return $dataProvider;
    }

截图来自巡洋舰先生

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/82461336
今日推荐