I have profile page view where user can change the current password. By using findBySql & current session I checked whether the current password is correct. But I don't know how to update the records in the model in yii framework.
You can simply follow this way to update a record in yii. $user = User::model()->findByPk($userId); $user->username = 'hello world'; $user->password = 'password'; $user->update(); How to save a new record in yii ? $user = new User(); $user->username = 'hello world'; $user->password = 'password'; $user->save();
Like other answers have pointed out, you can call update() on the builder object. Yii::$app->db->createCommand()->update('tableName', ['field' => 'value'], condition)->execute() . The 2nd parameter is a key-value pair of the fields you want to set and the values you want to set them to.
Yii Query Builder offers an object-oriented method for building SQL queries, which helps reduce risk of SQL injection attacks. And Yii Active Record (AR), implemented as a widely adopted Object-Relational Mapping (ORM) approach, further simplifies database programming.
You can simply follow this way to update a record in yii.
$user = User::model()->findByPk($userId);
$user->username = 'hello world';
$user->password = 'password';
$user->update();
How to save a new record in yii ?
$user = new User();
$user->username = 'hello world';
$user->password = 'password';
$user->save();
How to delete a record in yii ?
$user = User::model()->findByPk($userId);
$user->delete()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With