如何使用 Yii 2 ActiveRecord 做 IS NULL 和 IS NOT NULL?

2022-08-30 15:07:18

我有一个表,它有一个字段,这意味着它可以包含时间戳,也可以是,默认情况下。`activated_at` timestamp NULL DEFAULT NULLnullnull

我有另一个[gii生成的]搜索模型,该方法中具有以下配置:search()

public function search($params)
{
    $query = User::find();

    // add conditions that should always apply here

    $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;
    }

    $andFilterWhere = [
        'id' => $this->id,
        'status' => $this->status,
        'role' => $this->role,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
        'completed_files' => $this->completed_files,
        // 'activated_at' => null,
    ];

    if(!isset($_GET['deleted'])) {
        $query->where(['deleted_at' => null]);
        $andFilterWhere['deleted_at'] = null;
    } else if($_GET['deleted'] === 'true') {
        $query->where(['not', ['deleted_at' => null]]);
    }

    // grid filtering conditions
    $query->andFilterWhere(
        $andFilterWhere
    );

    $query->andFilterWhere(['like', 'first_name', $this->username])
        ->andFilterWhere(['like', 'auth_key', $this->auth_key])
        ->andFilterWhere(['like', 'password_hash', $this->password_hash])
        ->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])
        ->andFilterWhere(['like', 'email', $this->email])
        ->andFilterWhere(['like', 'first_name', $this->first_name])
        ->andFilterWhere(['like', 'last_name', $this->last_name]);

    if($this->activated || $this->activated === "0") {
        #die(var_dump($this->activated));
        if($this->activated === '1') {
            // this doesn't filter
            $query->andFilterWhere(['not', ['activated_at' => null]]);
        } else if($this->activated === '0') {
            // this doesn't either
            $query->andFilterWhere(['activated_at', null]);
        }
    }

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

    return $dataProvider;
}

是的,我已经在我的类中设置了属性:activated

public $activated;

我的方法如下:rules()

public function rules()
{
    return [
        [['id', 'status', 'role', 'created_at', 'updated_at', 'completed_files'], 'integer'],
        ['activated', 'string'],
        [['username', 'first_name', 'last_name', 'auth_key', 'password_hash', 'password_reset_token', 'email', 'deleted_at', 'completed_files', 'activated_at'], 'safe'],
    ];
}

我试图在方法中设置的是根据值对字段进行过滤(请参阅上面的代码):search()activated_at$activated

if($this->activated || $this->activated === "0") {
    #die(var_dump($this->activated));
    if($this->activated === '1') {
        // this doesn't filter
        $query->andFilterWhere(['not', ['activated_at' => null]]);
    } else if($this->activated === '0') {
        // this doesn't either
        $query->andFilterWhere(['activated_at', null]);
        $andFilterWhere['activated_at'] = null;
    }
}

我使用它 - 除了这个过滤器之外,所有其他过滤器都有效。GridView

我在这里做错了什么?

Aand如何正确地执行此类查询:

IS NULL something
IS NOT NULL something

使用 Yii 2 的查询生成器?ActiveRecord


编辑:Line:用于其他用途,这通常有效。if(!isset($_GET['deleted']))


答案 1

如果我理解正确,你可以使用和在哪里

 ->andWhere(['not', ['activated_at' => null]])

但是和Filter在执行中,其中相关值不为空

来自 doc http://www.yiiframework.com/doc-2.0/yii-db-query.html

andFilterWhere() 向现有条件添加一个附加的 WHERE 条件,但忽略空操作数。


答案 2

对于此表达式:

WHERE activated_at IS NULL

试试这个(它正在工作):

->andWhere(['is', 'activated_at', new \yii\db\Expression('null')]),

推荐