Comparison of findfindAll in the Yii framework to find the specified fields _ php instance

Source: Internet
Author: User
In the yii framework, you can use the find method of the ing Class to retrieve a piece of data or use the findall method to retrieve several pieces of data. Then, how to retrieve the data according to the required conditions is mainly used in the CDbCriteria class, this class is a well-known support class for yii operating databases.

ModelName: model ()-> find () // identifies an object.
ModelName: model ()-> findALL () // find the array of an object set
How can I find out the data of all fields instead of the data of all fields?

I did this before.

$criteria = new CDbCriteria;$criteria->select = 'username,id,email';$criteria->order = 'id DESC';$users = modelName::model()->findAll( $criteria );

The background accidentally saw what others wrote and how ignorant they were.

$users = modelName::model()->findAll(array(  'select' =>array('username','id','email'),  'order' => 'id DESC', ));

After the test, it is found that it can be used, so find can also perform this operation.

$user = modelName::model()->find(array(  'select' =>array('username','id','email'),  'order' => 'id DESC',  'condition' => 'id='.$id,));

Of course, it is definitely not safe to do so. The method below is the same.

$users = $this->user->find(array(  'select'=>array('id','username','email'),  'order' => 'id DESC',  'condition' => 'state=:state AND id=:id',  'params' => array(':state'=>'1',':id' => '2'),));

Similarly, you can use findAll to test it. Conclusion:

This method can easily obtain the required data. Of course, the following CDbCriteria

For example, if you want to retrieve 'v _ id', 'title', 'Big _ class', 'sub _ class', 'upload _ time' from the videoinfo table ', for fields such as 'comment _ num' with the condition status = 1, sort the lastmodifytime in reverse order and retrieve only three items. The operation is as follows:

$ Criteria = new CDbCriteria (); $ criteria-> select = array ('v _ id', 'title', 'Big _ class', 'sub _ class ', 'upload _ time', 'comment _ num'); $ criteria-> condition = 'status = 1'; $ criteria-> order = 'lastmodifytime desc '; $ criteria-> limit = 3; $ criteria-> params = array (': status' => $ your variable); $ result = VideoInfo: model () -> findAll ($ criteria );

The line I commented out can be represented by placeholders. For example, if your status needs to be assigned values based on the variables, you can assign values in the comment line, then write in the condition

$criteria -> condition = 'status = :status'; 

You can,
In this way, the result you get when the $ result variable is a list of objects and needs to be traversed:

foreach ($result as $ob){       print_r($ob->attributes);  } 

For example, if you want to display each field, you only need to input

$ob->attributes['title']; 

Wait.

The CPagination class can be used together with the CDbCriteria class and the front-end paging plug-in to support paging:

$ Count = VideoInfo: model ()-> count ($ criteria) $ pages = new CPagination ($ count); $ pages-> pageSize = 30; // The number of entries on each page. $ pages-> applyLimit ($ criteria );

$result = VideoInfo::model()->findAll($criteria); 

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.