Как обновить записи в БД Yii

Подскажите, как обновить колонку age у всех записей в БД у которых category = 2; Патаюсь так:

//Так обновляет все записи без условия
$user = User::updateAll(['age' => '21']);

//Так обновляет только колонку age в одной записи у которой категория = 2 
$user = user::findOne(['category' => '2']);
$user->age = '21';
$user->save();


Ответы (1 шт):

Автор решения: splash58

Судя по документации

https://www.yiiframework.com/doc/api/2.0/yii-db-activerecord#updateAll()-detail

Updates the whole table using the provided attribute values and conditions.

For example, to change the status to be 1 for all customers whose status is 2:

Customer::updateAll(['status' => 1], 'status = 2');

Warning: If you do not specify any condition, this method will update all rows in the table.

 User::updateAll(['age' => '21'], 'category = 2');
→ Ссылка