Search code examples
phpyiiyii2yii2-model

Yii 2 Update All Not IN


Based on the code below, I want to update all in customer to status 1 where status = 2. However, i want to run the code below where customer_id not in (1, 3, 5, 8).

$customerNotIn = array(1, 3, 5 ,8);
Customer::updateAll(['status' => 1], 'status = 2');

How can i achieve that?


Solution

  • The condition can be in the format of what you'd put in a ->where(), so in your case would be:

    $customerNotIn = array(1, 3, 5 ,8);
    Customer::updateAll(['status' => 1], ['AND', 
        'status = 2', 
        ['NOT IN', 'status', $customerNotIn]
    ]);