Detailed description of yii CURD operation instances

Source: Internet
Author: User
This article mainly introduces the CURD operation skills of yii, and analyzes in detail the usage of adding, deleting, modifying, and querying based on the yii Framework in the form of instances, which is of great practical value, for more information about yii CURD, see the following example. Share it with you for your reference. The specific analysis is as follows:

CURD is a acronym in database technology. Generally, the basic functions of various parameters in project development are CURD. It represents Create, Update, Retrieve, and Delete operations. This article describes the CURD operations of the Yii Framework.

I. query data sets

1,

The code is as follows:

$ Admin = Admin: model ()-> findAll ($ condition, $ params );

This method queries a set based on a condition, such:

The code is as follows:

FindAll ('username =: name', array (': name' => $ username ));

2,

The code is as follows:

$ 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:

The code is as follows:

FindAllByPk (array (1, 2 ));

3,

The code is as follows:

$ 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:

The code is as follows:

FindAllByAttributes (array ('username' => 'admin '));

4,

The code is as follows:

$ Admin = Admin: model ()-> findAllBySql ($ SQL, $ params );

This method queries an array based on SQL statements, such:

The code is as follows:

FindAllBySql ('select * from admin whereusername =: name', array (': name' => 'admin '));

II. method for querying objects

1,

The code is as follows:

$ Admin = Admin: model ()-> findByPk ($ postID, $ condition, $ params );

Query an object based on the primary key, for example, findByPk (1 );

2,

The code is as follows:

$ Row = Admin: model ()-> find ($ condition, $ params );

A group of data may be queried based on one condition, but only the first row of data is returned, for example:

The code is as follows:

Find ('username =: name', array (': name' => 'admin '));

3,

The code is as follows:

$ Admin = Admin: model ()-> findByAttributes ($ attributes, $ condition, $ params );

This method is used to query a group of data based on conditions. multiple conditions can be used to put conditions into the array. The first data queried by the condition is also the first data, for example:

The code is as follows:

FindByAttributes (array ('username' => 'admin '));

4,

The code is as follows:

$ Admin = Admin: model ()-> findBySql ($ SQL, $ params );

This method is used to query a group of data based on an SQL statement. it Also queries the first data, such:

The code is as follows:

FindBySql ('select * from admin whereusername =: name', array (': name' => 'admin '));

5. spell out a method to obtain SQL, and query an object based on find.

The code is as follows:

$ Criteria = new CDbCriteria;
$ 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,

The code is as follows:

$ N = Post: model ()-> count ($ condition, $ params );

This method queries the number of records in a set based on a condition and returns an int number, as shown in figure

The code is as follows:

Count ('username =: name', array (': name' => $ username ));

2,

The code is as follows:

$ N = Post: model ()-> countBySql ($ SQL, $ params );

This method queries the number of records in a set based on SQL statements and returns an int number, as shown in figure

The code is as follows:

CountBySql ('select * from admin whereusername =: name', array (': name' => 'admin '));

3,

The code is as follows:

$ Exists = Post: model ()-> exists ($ condition, $ params );

This method is used to query the queried array based on a condition. if any data exists, a true value is returned. otherwise, no data is found.

IV. Add methods

The code is as follows:

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

5. modification method

1,

The code is as follows:

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 'modification failed ';
}

2,

The code is as follows:

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,

The code is as follows:

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,

The code is as follows:

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,

The code is as follows:

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 ';
}

I hope this article will help you design PHP programs based on the Yii Framework.

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.