save() OR update()
Using model methods, You can load existing record and update the necessary changes in that.
$model = User::find($id);
$model->name = 'YII';
$model->email = 'yii2@framework.com';
$model->save(); // equivalent to $model->update();
Update Multiple Records
Using the below methods, You can update the entire table records using given attribute values and condition.To increment all the users age by 1,
User::updateAllCounters(['age' => 1]);
To change all the users status to active
User::updateAll(['status' => 1], ['like', 'email', '@dummy.com']);
update() command
Instead of writing plain UPDATE query, you can call update() function to change the records. execute() function will complete the update process.Syntax
$connection->createCommand()
->update('table_name', [SET_Values], 'CONDITION')
->execute();
$connection->createCommand()
->update('tbl_user', ['status' => 1], 'age > 30')
->execute();
Update By Sql Query
To run the plain UPDATE query, you can use `createCommand()` function.
$command = $connection->createCommand('UPDATE tbl_user SET status=1 WHERE userid=1');
$command->execute();
-
Nickolay Zuev
-
Damianos Giankakis
-
yatin mistry
-
Ahmad AL-Zahabi