Search code examples
phpyii2yii2-model

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.


Solution

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

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