Yii database addition, modification, and deletion operations Summary

Source: Internet
Author: User

Yii database addition, modification, and deletion operations Summary

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.