Yii 2.0 Framework Model Adding and Modifying and Deleting Data Operation Example
- 2021-12-13 07:37:27
- OfStack
This article illustrates the Yii 2.0 framework model adding/modifying/deleting data operations. Share it for your reference, as follows:
Add data
$user = new User();
$user->name = 'zhang';
$user->age = 28;
$data = $user->save();// Add data
$data = $user->insert();// Add data
$id = $user->attributes['id'];// Gets the self-increment after the current added data id
Modify data
$user = $User::findOne(1);
$user->name = 'zhang';
$data = $user->update();// Modify
$data = $user->save();// Modify
// Modify a single field
$data = Uesr::updateAllCounters(['name' => 'li'],['id' => 1]);// Parameter 1 Is the field to modify, the parameter 2 To modify the condition
Delete data
// Delete data
$user = User::find()->where(['id' => 2])->one();// Get id For 2 Data of
$data = $user->delete();// Delete data
$uer = User::find()->where(['id' => 2])->all();// Get id For 2 Data of
$data = $user[0]->delete();// Delete data
// Delete all data
$user = User::deleteAll();
$user = User::deleteAll('id=2');// Delete id For 2 Data of
// Multiple conditional deletions
$data = User::deleteAll('id>:id AND num<:num',[':id' => 2,':num' => 100]);
For more readers interested in Yii related contents, please check the topics on this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Basic Tutorial of Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
Hope that this article is based on Yii framework of PHP programming help.