MiglyCH
MiglyCH

Reputation: 49

yii2 AcrtiveRecord whereNot

I have a following SQL query which I want to "build" with the ORM of Yii2:

SELECT * FROM table WHERE [some conditions] AND (col1 <> 0 OR col2 <> 0)

So I want to exclude all results where col1 and col2 equals 0, but I don't want to do this with the SQL EXCEPT command.

The SQL should be correct, but my question is now how to build that with the yii2 ORM.

Upvotes: 0

Views: 69

Answers (2)

gud3
gud3

Reputation: 565

You need to use condition in one array with key 'OR'

Model::find()
        ->where(['condition' => 1])
        ->andWhere([
            'OR',
            ['!=', 'col1', 'val1'],
            ['!=', 'col2', 'val2'],
        ])
        ->all();

Upvotes: 1

Mike Kaharlykskiy
Mike Kaharlykskiy

Reputation: 121

Use this code:

Model::find()->where(['condition' => 1])
             ->andWhere(['condition2' => 20])
             ->andWhere(['not', ['col1' => 0]])
             ->andWhere(['not', ['col2' => 0]])
             ->all();

Upvotes: -2

Related Questions