Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of using Model query in Yii

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the use of Model query in Yii". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the ways to use Model queries in Yii?"

For a Model Post, there are 4 query methods that return objects or an array of objects.

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

If the find method finds a row that meets the query criteria, it returns an instance of Post whose properties contain the values of the corresponding column in the row of the data table. Then we can read the loaded values just as we would read the properties of a normal object, such as echo $post- > title;. If nothing is found in the database using the given query criteria, the find method returns null.

When we call find, we use $condition and $params to specify the query criteria. Here $condition can be a WHERE string in a SQL statement, and $params is an array of parameters where the value should be bound to a placeholder in $condation. For example, suppose we query the data with postID = 10

/ / find the row with postID=10$ post=Post::model ()-> find ('postID=:postID', array (': postID'= > 10))

The condition $condition is the where part of our sql. What about the parameter? it is passed through params, but the name is added with ":".

YII has a CDbCriteria class to construct the query, if we query a title,CdbCriteria with a postId of 10

$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 above example can be rewritten as follows:

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

Of course, it also applies to findAll ()

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

We can use findByAttributes () when a query condition is about matching several columns by a specified value. We make the $attributes parameter an array of values indexed by the column name.

The $attributes in findByAttributes is the name of the field. Query title for abc how to query? See below

The copy code is as follows:

Post::model ()-> findByAttributes (array ('title'= >' abc'))

Other methods:

1. $admin=Admin::model ()-> findAll ($condition,$params)

This method queries a collection according to a condition, such as:

The copy 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 collection based on the primary key, and you can use multiple primary keys, such as:

The copy code is as follows:

FindAllByPk (array (1pm 2))

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

This method queries a collection according to conditions, which can be multiple conditions, and puts the conditions into an array, such as:

The copy code is as follows:

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

4. $admin=Admin::model ()-> findAllBySql ($sql,$params)

This method queries an array based on the SQL statement, such as:

The copy code is as follows:

FindAllBySql ("select * from admin where username=:name", array (': name'= > 'admin'))

Second, the method of querying the object

1. $admin=Admin::model ()-> findByPk ($postID,$condition,$params)

Query an object according to the primary key, such as:

The copy code is as follows:

FindByPk (1)

2. $row=Admin::model ()-> find ($condition,$params)

Query a set of data according to a condition, possibly multiple, but it only returns the first row of data, such as:

The copy code is as follows:

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

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

This method is to query a set of data according to conditions, which can be multiple conditions, and put the conditions into an array, and he also queries the first piece of data, such as:

The copy code is as follows:

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

4. $admin=Admin::model ()-> findBySql ($sql,$params)

This method queries a set of data according to the SQL statement, and it also queries the first piece of data, such as:

The copy 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 according to 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

Third, the number of queries to determine whether the query has a result

1. $n=Post::model ()-> count ($condition,$params)

This method queries how many records there are in a collection according to a condition and returns an int number, such as

The copy code is as follows:

Count ("username=:name", array (": name" = > $username))

2. $n=Post::model ()-> countBySql ($sql,$params)

This method queries how many records there are in a collection according to the SQL statement and returns an int number, such as

The copy code is as follows:

CountBySql ("select * from admin where username=:name", array (': name'= > 'admin'))

3. $exists=Post::model ()-> exists ($condition,$params)

This method is based on a condition to query whether there is any data in the array. If any data is returned, a true will not be found.

Fourth, the method of adding

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

V. the method of modification

1. Post::model ()-> updateAll ($attributes,$condition,$params)

$count = Admin::model ()-> updateAll (array ('username'= >' 11111), 'password=:pass',array (': pass'= > '1111a1')); if ($count > 0) {echo "modified successfully";} else {echo "modified failed";}

2. Post::model ()-> updateByPk ($pk,$attributes,$condition,$params)

$count = Admin::model ()-> updateByPk (1 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 "modified failed";}

The competition represents the primary key, which can be either a collection or a collection, $attributes represents the collection of fields to be modified, $condition represents the condition, and the value passed in by $params

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 "modified failed";}

Array ('status'= > 1) represents the admin table in the database. All the result status fields queried by conditional username='admin', are incremented by 1.

VI. Method of deletion

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 if ($count > 0) {echo "delete success";} else {echo "deletion failed";}

2. Post::model ()-> deleteByPk ($pk,$condition,$params)

$count = Admin::model ()-> deleteByPk (1); $count = Admin::model ()-> deleteByPk (array (1Power2), 'username=:name',array (': name'= > 'admin')); if ($count > 0) {echo "deletion success";} else {echo "deletion failure";} so far, I believe you have a deeper understanding of "what is the use of Model query in Yii". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report