Example of adding deletion, modification, and search for yii data

Source: Internet
Author: User
Tags yii

There are many implementation methods for yii database operations. The following summarizes a set of statements for yii database operations:

1. Query data (select)

The code is as follows: Copy code

$ Con = Yii: app ()-> db;
// Query a row of data
$ NoteInfo = $ con-> createCommand ("select * from note where note_id = '{$ nid}'")-> queryRow ();
// Query multiple rows of data
$ NoteInfo = $ con-> createCommand ("select * from note where note_id = '{$ nid}'")-> queryAll ();

Query data sets

The code is as follows: Copy code

1. $ admin = Admin: model ()-> findAll ($ condition, $ params );
This method queries a set based on a condition, such:
FindAll ("username =: name", array (": name" => $ username ));

2. $ admin = Admin: model ()-> findAllByPk ($ postIDs, $ condition, $ params );
FindAllByPk ($ id, "name like ': name' and age =: age", array (': name' => $ name, 'age' => $ age ));
This method queries a set based on the primary key. Multiple primary keys can be used, for example:
FindAllByPk (array (1, 2 ));

3. $ admin = Admin: model ()-> findAllByAttributes ($ attributes, $ condition, $ params );

This method is used to query a set based on conditions. Multiple conditions can be used to put conditions in an array, for example:
FindAllByAttributes (array ('username' => 'admin '));

4. $ admin = Admin: model ()-> findAllBySql ($ SQL, $ params );
This method queries an array based on SQL statements, such:
FindAllBySql ("select * from admin whereusername =: name", array (': name' => 'admin '));


2. insert)

$ Con = Yii: app ()-> db;
$ Datas ['title'] = Yii: app ()-> request-> getParam ('title ');
$ Datas ['content'] = Yii: app ()-> request-> getParam ('content ');;
$ Datas ['add _ time'] = time ();
// Warehouse receiving
$ Res = $ con-> createCommand ()-> insert ('note', $ datas );

$ Admin = newAdmin;
$ Admin-> username = $ username;
$ Admin-> password = $ password;
If ($ admin-> save ()> 0 ){
Echo "added successfully ";
} Else {
Echo "failed to add ";
}
 
 
 
$ UserLimit = new UserLimit ();
$ UserLimit-> item = 0.30000;
If ($ userLimit-> insert (){
 
}

3. Modify data (update)

The code is as follows: Copy code

$ Con = Yii: app ()-> db;
$ Content = 'abc ';
$ Nid = '000000 ';
$ Con-> createCommand ()-> update ('note ',
Array (
'Content' => $ content,
'Last _ update' => time ()
), 'Note _ id =: nid', array (': nid' => $ nid ));

$ UserLimitRet = UserLimit: model ()-> findByPk (array (
'User _ id' => $ userId,
'Category _ id' => $ v
));
$ UserLimitRet-> order = $ order;
If (! $ UserLimitRet-> update ()){
}
 
 
 
 
 
$ UserAmount = userAmount: model ()-> findByPk ($ userId );
$ UserAmount-> credit = Yii: app ()-> request-> getParam ('credentials', 10000 );
$ Ret = $ userAmount-> save ();
 
 
 

1. Post: model ()-> updateAll ($ attributes, $ condition, $ params );
$ Count = Admin: model ()-> updateAll (array ('username' => '000000', 'password' => '000000'), 'password =: pass', array (': pass' => '1111a1 '));
If ($ count> 0 ){
Echo "modified successfully ";
} Else {
Echo "failed to modify ";
}
 
 
 
 
 
$ Ret = $ this-> updateAll (array (
'Set' => $ set
), "User_id in (". $ userIds. ") and category_id =". $ categoryId );
If ($ ret ){
Return true;
} Else {
Return false;
}
 
2. Post: model ()-> updateByPk ($ pk, $ attributes, $ condition, $ params );
$ Count = Admin: model ()-> updateByPk (1, array ('username' => 'admin', 'password' => 'admin '));
$ Count = Admin: model ()-> updateByPk (array (1, 2), array ('username' => 'admin', 'password' => 'admin '), 'username =: name', array (': name' => 'admin '));
If ($ count> 0 ){
Echo "modified successfully ";
} Else {
Echo "failed to modify ";

4. delete data)

The code is as follows: Copy code

$ Con = Yii: app ()-> db;
$ Con-> createCommand ()-> delete ('note', 'note _ id =: note_id ', array (': note_id '=> 123 ));

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.