Summary of database query operation in PHP YII framework

Source: Internet
Author: User
Tags yii
Yii is easy to learn and use. You just need to know PHP and object-oriented programming to get started quickly without having to learn a new schema or template language in advance. With Yii, the development speed is very fast, except for the framework itself, which requires very little coding to be written for the application. In fact, this is one of the most efficient development frameworks. The method of database query data under YII Framework how much do you know? Today's small series for everyone to do a summary.

A summary of the methods of database query data under the YII framework.

(1)

$result =yii::app ()->dbname->createcommand ($sql)->queryrow ();

The returned result set is a one-dimensional array if it is not empty. The dbname in the code is the name of the library in which the data table resides, $sql can be any SQL statement.

(2)

$result =yii::app ()->dbname->createcommand ($sql)->queryall ();

The returned result set is a two-dimensional array if it is not empty. The dbname in the code is the name of the library in which the data table resides, $sql can be any SQL statement.

(3)

$result =region::model ()->findbypk ($id);

The returned result is an object if it is not empty, and the region in the code represents the region.php file stored in the models directory, where the value of $id represents the value that the primary key defined in region.php is equal to.

(4)

$result =region::model ()->findall ($condition, $params);

For example:

FindAll ("Username=:name", Array (": name" = = $username));

The returned result set is a two-dimensional array if it is not empty, and the result set of the first dimension of the array is an object.

(5)

$admin =user::model ()->findallbypk ($postIDs, $condition, $params);

For example:

FINDALLBYPK ($id, "name like ': Name ' and Age=:age", Array (': Name ' = ' $name, ' age ' = $age));

The method is to query a collection based on the primary key, and the returned result set is a two-dimensional array if it is not empty, and the result set of the first dimension of the array is the object. You can also use multiple primary keys, such as: FINDALLBYPK (array);

(6)

$admin =user::model ()->findallbysql ($sql, $params);

The returned result set is a two-dimensional array if it is not empty, and the result set of the first dimension of the array is an object, using examples such as:

Findallbysql ("select * from admin where username=:name", Array (': Name ' = ' admin '));

(7)

$post =post::model ()->findbysql ($sql, $params);

The result set returned is an object if it is not empty, and the result of the query is only the first data. Such as:

Findbysql ("SELECT * from user where username=:name", Array (': Name ' = ' phpernote.com '));

(8)

$row =user::model ()->find ($condition, $params);

The result set returned is an object if it is not empty, and the result of the query is only the first data. Such as:

Find (' Username=:name ', Array (': Name ' = ' admin '));

Or:

Find (Array (' condition ' = ' user_id=:user_id ', ' params ' =>array (': user_id ' = $uid)));

(9)

$admin =user::model ()->findbyattributes ($attributes, $condition, $params);

The method is to query the data according to the condition, can be more than one condition, put the condition into the array, the query result set is an object and only one data, such as:

Findbyattributes (Array (' username ' = ' admin '));

Or:


(10)

$admin =user::model ()->findallbyattributes ($attributes, $condition, $params);

The returned result set is a two-dimensional array if it is not empty, and the result set of the first dimension of the array is an object.

(9)

If the query conditions are complex, use the Cdbcriteria class $criteria=new Cdbcriteria; $criteria->select= ' title '; $creteria->condition= ' postid=:p Ostid '; $criteria->params=array (':p ostid ' =>10); $post =post::model ()->find ($criteria); No need for a second parameter//another better notation $post=post::model ()->find (Array (        ' select ' = ' title ',        ' condition ' = ' = ' postid= :p Ostid ',        ' params ' =>array (':p ostid ' =>10))    ;

Query number, determine whether the query has results

1.

$n =post::model ()->count ($condition, $params);


The method is to query the number of records for a collection based on a condition, and return an int type, such as:

Count ("Username=:name", Array (": name" = = $username));


2.

$n =post::model ()->countbysql ($sql, $params);

The method is to query the number of records for a collection based on the SQL statement, returning an int number, such as:

Countbysql ("select * from admin where username=:name", Array (': Name ' = ' admin '));

3.

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

The method is based on a conditional query query to get the array with no data, if there is data returned by a true, otherwise not found.

The above is a summary of the method of database query data under the YII framework. I believe you have a more detailed understanding of this, we also mentioned earlier about the YII framework of more relevant articles, we can focus on topic.alibabacloud.com.

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.