Php 如何使用Yii 2 ActiveRecord实现为空和不为空?

Php 如何使用Yii 2 ActiveRecord实现为空和不为空?,php,activerecord,yii,yii2,Php,Activerecord,Yii,Yii2,我有一个表,它在timestamp NULL DEFAULT NULL处有一个字段`activated\,这意味着它可以包含一个时间戳,也可以是NULL,默认情况下是NULL 我有另一个[gii生成的]搜索模型,在search()方法中有以下配置: public function search($params) { $query = User::find(); // add conditions that should always apply here $this-

我有一个表,它在timestamp NULL DEFAULT NULL处有一个字段
`activated\,这意味着它可以包含一个时间戳,也可以是
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\u(参见上面的代码):

我将它与
GridView
一起使用-除此之外,其他所有过滤器都可以工作

我做错了什么

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

IS NULL something
IS NOT NULL something
使用Yii的
ActiveRecord
query builder



编辑:行:
如果(!isset($\u GET['deleted'])
用于其他用途,并且正常工作。

如果我理解正确,您可以使用andWhere

 ->andWhere(['not', ['activated_at' => null]])
但是andFilterWhere在execute中相关值不为null的位置

来自博士

andFilterWhere()将附加的WHERE条件添加到 现有操作数,但忽略空操作数

对于此表达式:

WHERE activated_at IS NULL
试试这个(它正在工作):


此解决方案检查
列\u name
是否为空或空

WHERE(长度(`column\u name`)>0)


另一个变量-仅检查NULL

其中列名称不为空

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


是的,我刚读过,我把我的答案贴在你的答案后面。不管怎样,我还是接受你的。谢谢嗨,欢迎来到StackOverflow。避免只使用代码回答问题,尝试解释问题是什么以及代码如何修复所述问题。
新表达式('NULL')
将同时适用于
->和where()
以及
->和filterwhere()
。如果你解释了你提供的代码是如何回答这个问题的,那么这将是一个更好的答案。这回答了一个查询,该查询过滤具有不同列名的NULL和notnull列,如问题中所问,其中status为10,type为NULL,info为notnull。我希望你能理解。TQ
->andWhere(['is', 'activated_at', new \yii\db\Expression('null')]),
->andWhere(['>', 'LENGTH(column_name)', 0]) //check if empty or null
->andWhere(['IS NOT', 'column_name', null]); // check on null
$null = new Expression('NULL');

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