For a Model Post, there are the following 4 query methods to return objects or object arrays.
The code is as follows: |
Copy code |
// Find the first row satisfying the specified condition $ Post = Post: model ()-> find ($ condition, $ params ); // Find the row with the specified primary key $ Post = Post: model ()-> findByPk ($ postID, $ condition, $ params ); // Find the row with the specified attribute values $ Post = Post: model ()-> findByAttributes ($ attributes, $ condition, $ params ); // Find the first row using the specified SQL statement $ Post = Post: model ()-> findBySql ($ SQL, $ params ); Suppose we query data with postID = 10. How can we query it? See the following: // Find the row with postID = 10 $ Post = Post: model ()-> find ('postid =: postid', array (': postid' => 10 ));
|
Condition $ condition is the where part of our SQL. What should we do with the parameter? It is passed through params, but the name is added.
YII has a CDbCriteria class to construct a query. If we query the title with a postId of 10, CdbCriteria is constructed in this way.
The code is as follows: |
Copy code |
$ Criteria = new CDbCriteria; $ Criteria-> select = 'title'; // only select the 'title' column $ Criteria-> condition = 'postid =: postid '; $ Criteria-> params = array (': postid' => 10 ); $ Post = Post: model ()-> find ($ criteria); // $ params is not needed You can also write it as follows: $ Post = Post: model ()-> find (array ( 'Select' => 'title ', 'Condition' => 'postid =: postid ', 'Params' => array (': postid' => 10 ), ));
|
In findByAttributes
$ Attributes is the name of the field.
How can I query the title abc? See the following
Post: model ()-> findByAttributes (array ('title' => 'ABC '))
Other methods:
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 where username =: name", array (': name' => 'admin '));
|
-----------------------------------------------------------------------------
II. Method for querying objects
The code is as follows: |
Copy code |
1. $ admin = Admin: model ()-> findByPk ($ postID, $ condition, $ params ); Query an object based on the primary key, for example, findByPk (1 ); 2. $ 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: Find ('username =: name', array (': name' => 'admin ')); 3. $ 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: 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. It Also queries the first data, such: FindBySql ("select * from admin where username =: name", array (': name' => 'admin '));
|
5. Spell out a method to obtain SQL, and query an object based on find.
The code is as follows: |
Copy code |
$ 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 is not needed
|
------------------------------------------------------------------------------
III. Number of queries to determine whether the query has results
The code is as follows: |
Copy code |
1. $ 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 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 and returns an int number, as shown in figure CountBySql ("select * from admin where username =: name", array (': name' => 'admin ')); 3. $ 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: |
Copy code |
$ Admin = new Admin; $ Admin-> username = $ username; $ Admin-> password = $ password; If ($ admin-> save ()> 0 ){ Echo "added successfully "; } Else { Echo "failed to add "; }
|
========================================================== =====
5. Modification method
The code is as follows: |
Copy code |
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 "; } 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 "; } $ 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 "failed to modify "; } 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 incremented by 1.
|
========================================================== ============
VI. Deletion method
The code is as follows: |
Copy code |
1. Post: model ()-> deleteAll ($ condition, $ params ); $ Count = Admin: model ()-> deleteAll ('username =: name and password =: 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 "failed to delete "; } 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 "failed to delete "; } |