Search code examples
yii2yii2-advanced-appyii2-model

Not equal condition with findAll()


I am trying to pull record from a table using the following code

$userId = Yii::$app->user->id;
$lists = PromoLists::findAll(['user_id' => $userId, 'list_type' => 'custom']);

which outputs a query like below

select * from promo_lists where user_id ='$userId' and list_type='custom'

But i am unable to find any thing in the documentation that would help me achieve it with the following condition.

select * from promo_lists where user_id ='$userId' and list_type='custom' and status!='deleted'

as the status is an ENUM field and there are 4 different status

'active','pending','rejected','deleted'

currently i used the following approach

PromoLists::findAll(['user_id' => $userId, 'list_type' => 'custom', 'status'=>['active','pending','rejected']]);

which outputsthe following query

select * from promo_lists where user_id ='$userId' and list_type='custom' and status in ('active','pending','rejected')

which somehow achieves the same thing but this query would need to be edited every time when there is a new status type added to the table column status.

i know i can do this by using PromoLists::find()->where()->andWhere()->all()

but how to check with != / <> operator using findAll().


Solution

  • Simply like this:

    PromoLists::find()->where(['and',
        [
            'user_id' => $userId, 
            'list_type' => 'custom',
        ],
        ['<>', 'status', 'deleted'],
    ])->all();