Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update record in model- YII Framework

Tags:

yii

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.

like image 203
balaji587 Avatar asked Dec 11 '13 12:12

balaji587


People also ask

How to update a Record in Yii?

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 update table in Yii2?

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.

What is ORM in Yii?

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.


1 Answers

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()
like image 117
dev1234 Avatar answered Sep 21 '22 17:09

dev1234