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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Yii 2 ActiveRecord执行IS NULL和IS NOT NULL?相关的知识,希望对你有一定的参考价值。

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

我在search()方法中有另一个[gii-generated]搜索模型,其配置如下:

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一起使用 - 除了这个之外,其他每个过滤器都有效。

我在这做错了什么?

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

IS NULL something
IS NOT NULL something

使用Yii 2的ActiveRecord查询构建器?


编辑:行:if(!isset($_GET['deleted']))用于其他东西,这是正常的。

答案

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

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

但是,在执行的地方,相关值不为空

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

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

另一答案

对于这个表达式:

WHERE activated_at IS NULL

尝试这个(它的工作):

->andWhere(['is', 'activated_at', new yiidbExpression('null')]),

以上是关于如何使用Yii 2 ActiveRecord执行IS NULL和IS NOT NULL?的主要内容,如果未能解决你的问题,请参考以下文章

Yii2 - ActiveRecord 到数组

如何将 DAO 查询转换为 Yii2 ActiveRecord。如何从连接表中选择和使用特定列

如何在 YII2 ActiveRecord 查询中说 (a AND b) OR (c AND d)?

yii2 使用 ActiveRecord 批量插入

没有 yii 的 Yii Activerecord

Yii2:具有不同查询的 ActiveRecord