Yii Use find findAll to find the implementation of the specified field

Source: Internet
Author: User

Yii Use find findAll to find the implementation of the specified field

This article mainly introduces the implementation of Yii using find findAll to find the specified field. It is a very practical technique. For more information, see.

Anyone who has used Yii knows that the following method is used:

ModelName: model ()-> find () // identifies an object modelName: model ()-> findALL () // identifies an array of an object set

We can find the array of objects and object sets. How can we find the data of all fields instead of the data of all fields? Previously I did this:

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

When someone else accidentally writes this in the background, the method is quite good:

$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, this operation is definitely not safe. You can use the following method:

$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 for testing.

Conclusion:

In this way, you can easily obtain the required data. Of course, you still need to create CDbCriteria when paging is required.

Articles you may be interested in
  • Yii view (output) the SQL statement executed on the current page
  • Module Development and Analysis of Yii framework
  • Yii controller action parameter binding
  • Yii database addition, modification, and deletion operations Summary
  • Convert the result of yii object to an array
  • Detailed analysis on the directory structure of the yii framework
  • Yii framework cache knowledge Summary
  • Yii database query Operation Summary

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.