Yii uses find FindAll to find out how to implement a specified field

Source: Internet
Author: User
Tags yii

Yii uses find FindAll to look up the implementation of the specified field, very practical skills, the needs of friends can refer to the next.

Friends who have used Yii know that the following methods are used:

View Code Printing
1 modelName::model()->find() //找出的是一个对象
2 modelName::model()->findALL() //找出的是一个对象集合的数组

You can find an array of objects and objects, so how do I find the data for the fields I need instead of the data for all the fields? That's what I did before:

View Code Printing
1 $criteria=newCDbCriteria;
2 $criteria->select=‘username,id,email‘;
3 $criteria->order=‘id DESC‘;
4 $users=modelName::model()->findAll( $criteria);

Backstage to see others have so written, the method is very good:

View Code Printing
1 $users=modelName::model()->findAll(array(‘select‘=>array(‘username‘,‘id‘,‘email‘),‘order‘=>‘id DESC‘));

After the test is found to work, then find can also do this:

View Code Printing
1 $user=modelName::model()->find(array(‘select‘=>array(‘username‘,‘id‘,‘email‘),‘order‘=>‘id DESC‘,‘condition‘=>‘id=‘.$id, ));

Sure, it's not safe to do this, but the following method can also be used:

View Code Printing
1 $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‘)));

It is also possible to test with FindAll.

Conclusion:

This method can be very convenient to obtain the required data, of course, the need for paging or new Cdbcriteria.

Yii uses find FindAll to find out how to implement a specified field

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.