How to do IS NULL and IS NOT NULL with Yii 2 ActiveRecord? : <p>I have a table which has a field <code>`activated_at` timestamp NULL DEFAULT NULL</code>, which means that it can contain a timestamp or it can be <code>null</code> and it's <code>null</code> by default.</p>

<p>I have another [gii-generated] search model with a following configuration in the <code>search()</code> method:</p>

<pre><code>public function search($params)
{
    $query = User::find();

    // add conditions that should always apply here

    $this-&gt;load($params);

    if (!$this-&gt;validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query-&gt;where('0=1');
        return $dataProvider;
    }

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

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

    // grid filtering conditions
    $query-&gt;andFilterWhere(
        $andFilterWhere
    );

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

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

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

    return $dataProvider;
}
</code></pre>

<p>Yes, I have set the <code>activated</code> property in my class:</p>

<pre><code>public $activated;
</code></pre>

<p>And my <code>rules()</code> method is as following:</p>

<pre><code>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'],
    ];
}
</code></pre>

<p>What I was trying to set in the <code>search()</code> method is to filter on field <code>activated_at</code> depending on the <code>$activated</code> value (see above code):</p>

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

<p>I use it with <code>GridView</code> - every other filter works except this one.</p>

<p>What am I doing wrong here?</p>

<p>Aand how to properly do this sort of queries:</p>

<pre><code>IS NULL something
IS NOT NULL something
</code></pre>

<p>With Yii 2's <code>ActiveRecord</code> query builder?</p>

<hr>

<p><strong>EDIT:</strong> Line: <code>if(!isset($_GET['deleted']))</code> is used for something else and this works normally.</p>
