Vaseem Khan
Vaseem Khan

Reputation: 51

Yii2 : how to use deleteAll with two conditions = and NOT IN

I am trying to delete data from RestoFoods model like that:

RestoFoods::deleteAll(["restaurant_id"=>$postData['resto_id'], 'food_id NOT IN'=> [1,2] ]);

I want this sql:

DELETE FROM `resto_foods` WHERE `restaurant_id`=1 AND (`food_id` NOT IN (1, 2));

Upvotes: 5

Views: 8592

Answers (1)

GAMITG
GAMITG

Reputation: 3818

You can try this way

RestoFoods::deleteAll(['AND', 'restaurant_id = :restaurant_id', ['NOT IN', 'food_id', [1,2]]], [':restaurant_id' => $postData['resto_id']]);

Output for this will be you want:

DELETE FROM `resto_foods` WHERE (restaurant_id = 1) AND (`food_id` NOT IN (1, 2));

Upvotes: 9

Related Questions