Yii database addition, modification, and deletion operations summary. Yii database addition, modification, and deletion operations summary This article summarizes the operations related to adding data, modifying data, and deleting data in yii. just a few days ago, yii database addition, summary of modification and deletion operations
This article summarizes the operations related to adding data, modifying data, and deleting data in yii. I just learned a few days and only recorded some data. I will continue to enrich it later, if you need it, you can check it out.
How to add data
(1) save method (object-based operation)
$ User = new User;
$ User-> username = 'phpernote ';
$ User-> password = '000000 ';
If ($ user-> save ()> 0 ){
Echo 'added successfully ';
} Else {
Echo 'add failed ';
}
(2) insert method (array operation)
Yii: app ()-> dbName-> createCommand ()-> insert ('tbl _ user ',
Array (
'Username' => 'phpernote ',
'Password' => '123'
)
);
(3) insert method (object-based operation)
$ User = new User ();
$ User-> username = 'phpernote ';
$ User-> password = '000000 ';
If ($ user-> insert ()){
Echo 'added successfully ';
} Else {
Echo 'add failed ';
}
How to modify data
(1) Post: model ()-> updateAll ($ attributes, $ condition, $ params );
$ Count = User: model ()-> updateAll (array ('username' => 'phpernote', 'password' => '000000'), 'id =: ID', array (': ID' => $ id ));
If ($ count> 0 ){
Echo 'modified successfully ';
} Else {
Echo 'modification failed ';
}
(2) Post: model ()-> updateByPk ($ pk, $ attributes, $ condition, $ params );
$ Count = User: model ()-> updateByPk (1, array ('username' => 'admin', 'password' => '123 '));
Or
$ Count = User: model ()-> updateByPk (array (123456), array ('username' => 'admin', 'password' => '123 '), 'username =: name', array (': name' => 'admin '));
If ($ count> 0 ){
Echo 'modified successfully ';
} Else {
Echo 'modification failed ';
}
$ Pk indicates the primary key, either a set or a set, $ attributes indicates the set of fields to be modified, $ condition indicates the condition, and $ params indicates the input value.
(3) Post: model ()-> updateCounters ($ counters, $ condition, $ params );
$ Count = User: model ()-> updateCounters (array ('status' => 1), 'username =: name', array (': name' => 'admin '));
If ($ count> 0 ){
Echo 'modified successfully ';
} Else {
Echo 'modification failed ';
}
Array ('status' => 1) indicates the admin table in the database. according to the condition username = 'admin', the status field of all the queried results is automatically added with 1
User: model ()-> updateCounters (array ('count' => 1), 'id = '. user: model ()-> id); // auto-Increment 1
User: model ()-> updateCounters (array ('count' =>-1), 'id = '. user: model ()-> id); // auto-decrease 1
(4) Yii: app ()-> dbName-> createCommand ()-> update ($ attributes, $ condition, $ params );
Yii: app ()-> dbName-> createCommand ()-> update ('tbl _ user ',
Array (
'Username' => 'phpernote'
),
'Id =: ID ',
Array (
': ID' => 3
)
);
How to delete data
(1) Post: model ()-> deleteAll ($ condition, $ params );
For example:
$ Count = User: model ()-> deleteAll ('username =: name AND password =: pass', array (': name' => 'phpernote ',': pass '=> '123 '));
Or:
$ Count = User: model ()-> deleteAll ('Id in (", 3") '); // delete the data whose id is
If ($ count> 0 ){
Echo 'deleted successfully ';
} Else {
Echo 'deletion failed ';
}
(2) Post: model ()-> deleteByPk ($ pk, $ condition, $ params );
For example:
$ Count = User: model ()-> deleteByPk (1 );
Or:
$ Count = User: model ()-> deleteByPk (array (1, 2), 'username =: name', array (': name' => 'admin '));
If ($ count> 0 ){
Echo 'deleted successfully ';
} Else {
Echo 'deletion failed ';
}
Articles you may be interested in
- Reasons why PHP adds a backslash before the quotation mark and how PHP removes the backslash, there are three ways to disable the php magic quotation mark
- Mysql database cache function analysis, debugging, and Performance Summary
- Php mysql database operations
- FckEditor shortcut menu-image deletion function
- Summarize the causes and solutions for the slow MySQL database server
- Mysql server master-slave database synchronization configuration
- Php clears (deletes) the files in the specified directory, and does not delete the directory folder.
- Php uses array_flip to implement array key-value exchange to remove array duplicate values
Http://www.bkjia.com/PHPjc/980975.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/980975.htmlTechArticleyii database add, modify, delete related operation summary This article summarizes the yii on the data information to add data, modify data, delete data related operations, just learning a few days ,...