0

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().

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68

2 Answers2

2

Simply like this:

PromoLists::find()->where(['and',
    [
        'user_id' => $userId, 
        'list_type' => 'custom',
    ],
    ['<>', 'status', 'deleted'],
])->all();
Bizley
  • 17,392
  • 5
  • 49
  • 59
0

Using operator format in condition

http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html#operator-format

PromoLists::find()
    ->andWhere([
        'user_id' => $userId,
        'list_type' => 'custom',
        ['!=', 'status', 'deleted']
    ])
    ->all();
Ripper
  • 1,132
  • 13
  • 26
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Donald Duck Mar 08 '17 at 11:24