Yii CURD (create, modify, read, and delete) operations

Source: Internet
Author: User

I. Query data sets

1. $ admin = Admin: model ()-> findAll ($ condition, $ params); this method is used to query a set based on a condition, for example, 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 and can use multiple primary keys, for example, findAllByPk (array (1, 2 ));

3. $ admin = Admin: model ()-> findAllByAttributes ($ attributes, $ condition, $ params). This method queries a set based on conditions and may contain multiple conditions, put the conditions in the 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 '));

II. Method for querying objects

1. $ admin = Admin: model ()-> findByPk ($ postID, $ condition, $ params); an object is queried based on the primary key, for example, findByPk (1 );

2. $ row = Admin: model ()-> find ($ condition, $ params); query a group of data based on one condition, which may be multiple, however, only the first row of data is returned, for example, find ('username =: name', array (': name' => 'admin '));

3. $ admin = Admin: model ()-> findByAttributes ($ attributes, $ condition, $ params). This method queries a set of data based on conditions, multiple conditions can be used to put the conditions in the array. The query result is also the first data, for example, findByAttributes (array ('username' => 'admin '));

4. $ admin = Admin: model ()-> findBySql ($ SQL, $ params). This method is used to query a group of data based on an SQL statement, which also queries the first data, for example, findBySql ('select * from admin whereusername =: name', array (': name' => 'admin '));

5. Spell out a method to obtain SQL, and query an object $ criteria = new CDbCriteria based on find;
$ Criteria-> select = 'username'; // only select the 'title' column
$ Criteria-> condition = 'username =: username ';
$ Criteria-> params = array (': username => 'admin ');
$ Post = Post: model ()-> find ($ criteria); // $ params isnot needed

III. Number of queries to determine whether the query has results

1. $ n = Post: model ()-> count ($ condition, $ params); this method queries the number of records in a set based on a condition, returns an int number, such as count ('username =: name', array (': name' => $ username ));

2. $ n = Post: model ()-> countBySql ($ SQL, $ params). This method queries the number of records in a set based on SQL statements, returns an int-type number, such as countBySql ('select * from admin whereusername =: name', array (': name' => 'admin '));

3. $ exists = Post: model ()-> exists ($ condition, $ params); this method is used to query the queried array based on a condition, returns true if data exists. Otherwise, no value is found.

IV. Add methods

$ Admin = newAdmin;
$ Admin-> username = $ username;
$ Admin-> password = $ password;
If ($ admin-> save ()> 0 ){
Echo 'added successfully ';
} Else {
Echo 'add failed ';
}

5. Modification method

1. Post: model ()-> updateAll ($ attributes, $ condition, $ params );
$ Count = Admin: model ()-> updateAll (array ('username' => '2017 & prime;, 'password' => '2017 & prime ;), 'password =: pass', array (': pass' => '1111a1 & prime ;));
If ($ count> 0 ){
Echo 'modified successfully ';
} Else {
Echo 'modification failed ';
}

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 '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 = Admin: model ()-> updateCounters (array ('status' => 1), 'username =: name', array (': name' => 'admin '));
If ($ count> 0 ){
Echo 'modified successfully ';
} Else {
Echo 'modification failed ';
}

Array ('status' = & gt; 1) indicates the admin table in the database. According to the condition username = 'admin', the status field of all the queried results is added with 1

VI. Deletion method

1. Post: model ()-> deleteAll ($ condition, $ params );
$ Count = Admin: model ()-> deleteAll ('username =: nameandpassword =: pass', array (': name' => 'admin ',': pass '=> 'admin '));
$ Id = 1, 2, 3
DeleteAll ('Id in ('. $ id.'); delete the data whose id is
If ($ count> 0 ){
Echo 'deleted successfully ';
} Else {
Echo 'deletion failed ';
}

2. Post: model ()-> deleteByPk ($ pk, $ condition, $ params );
$ Count = Admin: model ()-> deleteByPk (1 );
$ Count = Admin: model ()-> deleteByPk (array (1, 2), 'username =: name', array (': name' => 'admin '));
If ($ count> 0 ){
Echo 'deleted successfully ';
} Else {
Echo 'deletion failed ';
}

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.