Yii provides an example of a Model query technique based on arrays and objects. yiimodel_PHP tutorial

Source: Internet
Author: User
Yii provides detailed examples of the Model query technique based on arrays and objects, and yiimodel. Yii provides an example of the Model query technique based on arrays and objects. yiimodel describes the Model query technique of Yii based on arrays and objects. For your reference, Yii provides an example of the Model query technique based on arrays and objects. yiimodel

This article describes the Yii Model query technique based on arrays and objects. We will share this with you for your reference. The details are as follows:

For a Model Post, there are the following 4 query methods to return objects or object arrays.

// Find the first row satisfying the specified condition $ post = Post: model ()-> find ($ condition, $ params) in the result that meets the specified condition ); // find the row with the specified primary key value find the row with the specified primary key $ post = Post: model ()-> findByPk ($ postID, $ condition, $ params); // find the row with the specified attribute value. find the row with the specified attribute values $ post = Post: model ()-> findByAttributes ($ attributes, $ condition, $ params); // return null if not found // find the first row using the specified SQL statement $ post = Post in the query result using the specified SQL statement :: model ()-> findBySql ($ SQL, $ params );

If the find method finds a row that meets the query conditions, it returns a Post instance. the instance attributes contain the values of the corresponding columns in the row of the data table. Then we can read the loaded value like reading the attributes of a common object, such as echo $ post-> title ;. If nothing is found in the database using the given query conditions, the find method returns null.

When we call find, we use $ condition and $ params to specify the query conditions. $ Condition can be the WHERE string in the SQL statement, and $ params is a parameter array. The value should be bound to a placeholder in $ condation. For example, suppose we query data with postID = 10.

// 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.

$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

One alternative to CDbCriteria is to pass an array to the find method. The keys and values of the array correspond to the attribute names and values of the standard (criterion). the preceding example can be rewritten as follows:

$post=Post::model()->find(array(  'select'=>'title',  'condition'=>'postID=:postID',  'params'=>array(':postID'=>10),));

It also applies to findAll ()

self::$_items[$type]=array();$models=self::model()->findAll(array(  'condition'=>'type=:type',  'params'=>array(':type'=>$type),  'order'=>'position',));

When a query condition is about matching several columns by specified value, we can use findByAttributes (). The $ attributes parameter is an array of values indexed by column names.
$ Attributes in findByAttributes is the name of the field. how can I query the title abc? See the following
The code is as follows: Post: model ()-> findByAttributes (array ('title' => 'ABC '))

Other methods:

1. $ 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. $ 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. $ 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. $ 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 where username =: 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: The code is as follows: 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:
The code is as follows: 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:
The code is as follows: 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:
The code is as follows: 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.

$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

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
The code is as follows: 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
The code is as follows: 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

$ Admin = new Admin; $ admin-> username = $ username; $ admin-> password = $ password; if ($ admin-> save ()> 0) {echo "added successfully";} else {echo "failed to add ";}

5. modification method

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 "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 "modification succeeded";} 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 incremented by 1.

VI. deletion method

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. ") '); 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 "deletion failed ";}

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

Articles you may be interested in:
  • Php yii Framework Development Tips: rules custom validation rules in model (models)
  • Yii2 tips to use Composer to add FontAwesome font resources
  • YiiFramework knowledge point summary (graphic tutorial)
  • Summary of common log operations of PHP Yii Framework
  • Yii Learning Summary-Data Access Object (DAO)
  • Install and configure Yii Learning Summary
  • YII path usage summary
  • Yii tips

Examples in this article describes the Yii Model query technique based on arrays and objects. For your reference, please refer to the following details :...

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.