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

8wtpewkr  于 2022-11-09  发布在  其他
关注(0)|答案(5)|浏览(410)

我有一个表,其中有一个字段``activated_attimestamp NULL DEFAULT NULL,这意味着它可以包含一个时间戳,或者它可以是null,默认情况下是null
我有另一个[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的值对字段activated_at进行过滤(请参见上面的代码):

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']))用于其他用途,此操作正常工作。

koaltpgm

koaltpgm1#

如果我没理解错的话你可以用和Where

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

但执行中的andFilterWhere相关值不为空
来自文件http://www.yiiframework.com/doc-2.0/yii-db-query.html
andFilterWhere()将附加WHERE条件添加到现有WHERE条件中,但忽略空操作数。

lsmepo6l

lsmepo6l2#

对于此表达式:

WHERE activated_at IS NULL

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

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

ar7v8xwq3#

$null = new Expression('NULL');

$query->andFilterWhere(['is not', 'asp_id', $null]);

$query->andFilterWhere(['is', 'asp_id', $null]);
jutyujz0

jutyujz04#

该解决方案检查column_name是否为空或NULL
WHERE (LENGTH(column_name) > 0)

->andWhere(['>', 'LENGTH(column_name)', 0]) //check if empty or null

另一个变量-仅检查NULL
WHERE column_name IS NOT NULL

->andWhere(['IS NOT', 'column_name', null]); // check on null
rta7y2nd

rta7y2nd5#

// ...WHERE (`status` = 10) AND (`type` IS NULL) AND (`info` IS NOT NULL)
$query->where([
    'status' => 10,
    'type' => null,
])
->andWhere(['not', ['info' => null]]);

相关问题