A
A
Anton Shtinov2016-01-26 18:01:25
Yii
Anton Shtinov, 2016-01-26 18:01:25

How to update a table with a complex condition?

There is a table with two fields: id, active
And there is a list of id (id_list) that do not need to be updated. How to update only those records in the table that do not fall into the id_list list, and which have active=1
I have come up with the following options so far:
1) I make a selection of the necessary elements, and then update them one by one.

$data = Model::find()->where(['not in', 'id', $id_list])->andWhere(['active' => 1])->all();
foreach($data as $line) {
  // обновить элемент
}

2) I set the condition directly
Model::updateAll(['active' => 0], 'active = 1 AND id NOT IN ('.implode(',', $id_list).')');

But I don't like any of these options.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question