Home >PHP Framework >YII >DbCriteria in Yii framework: Query the database efficiently
DbCriteria in the Yii framework: Query the database efficiently
The Yii framework is a fast, efficient, and safe PHP framework. It provides a powerful database operation class DbCriteria that can help us query more efficiently. database and improve application performance. This article will introduce how to use DbCriteria for database queries.
Creation of DbCriteria
We can use the following code to create a DbCriteria instance:
$criteria = new CDbCriteria;
DbCriteria provides a streaming query method, which allows us to pass a chain Set query conditions by calling methods, for example:
$criteria->select('title, content') ->addCondition('status=:status') ->params(array(':status'=>1)) ->order('create_time DESC') ->limit(10);
In the above code, we use the select method to specify the fields to be queried, use the addCondition method to set the query conditions, use the params method to bind query parameters, and use The order method sorts the query results, and the limit method sets the number of query results.
DbCriteria provides a variety of query methods, including select, addCondition, params, order, limit and other methods. Below we will introduce these methods one by one.
select method
The select method is used to specify the field to be queried. It can receive one or more field names as parameters, for example:
$criteria->select('id, name, email');
It can also be an array of The form specifies the fields to be queried:
$criteria->select(array('id', 'name', 'email'));
addCondition method
The addCondition method is used to add query conditions. It can receive the following different parameters:
For example:
$criteria->addCondition('age>:age'); $criteria->addCondition('gender=:gender'); $criteria->params(array(':age'=>18, ':gender'=>'Female'));
In the above code, we use the addCondition method to add two query conditions, and use the params method to bind the query parameters.
params method
The params method is used to bind query parameters. It receives an array as a parameter. The key of the array represents the parameter name to be bound, and the value represents the parameter value to be bound. For example:
$criteria->params(array(':age'=>18, ':gender'=>'Female'));
In the above code, we use the params method to bind two query parameters: :age and :gender.
order method
The order method is used to sort the query results. It receives a string as a parameter, indicating the conditions for sorting, for example:
$criteria->order('create_time DESC');
In the above code, We use the order method to sort the query results in descending order according to the create_time field.
limit method
The limit method is used to limit the number of query results. It receives an integer as a parameter, indicating the number of query results, for example:
$criteria->limit(10);
In the above code, We use the limit method to limit the number of query results to 10 records.
Use of DbCriteria
After we create a DbCriteria instance, we can apply it to the query in the following two ways:
$model = Post::model()->find($criteria);
In the above code, we call the find method of the Post model class and pass the DbCriteria instance as a parameter to the method to query a single record.
$models = Post::model()->findAll($criteria);
In the above code, we call the findAll method of the Post model class and pass the DbCriteria instance as a parameter to the method to query multiple records.
Note: We can also use the query method to query using DbCriteria. For example:
$models = Yii::app()->db->createCommand() ->select('id, name, email') ->from('user') ->where('status=:status', array(':status'=>1)) ->order('create_time DESC') ->limit(10) ->queryAll();
In the above code, we obtain the database connection object through Yii::app()->db, use the createCommand method to create a command object, and then use select, from, where, order, limit and other methods to set the query conditions, and finally call the queryAll method to query.
Summary
DbCriteria is a very powerful database query tool in the Yii framework. It provides a series of easy-to-use methods to set query conditions, bind query parameters, sort query results, and limit The number of queries, etc., helps us query the database more efficiently and improve application performance. We should make full use of DbCriteria to optimize our code when making database queries.
The above is the detailed content of DbCriteria in Yii framework: Query the database efficiently. For more information, please follow other related articles on the PHP Chinese website!