Yii use Find FindAll finds the implementation method for the specified field _php instance

Source: Internet
Author: User
Tags yii

In this paper, the example shows that Yii uses find FindAll to locate the specified field and share it for everyone's reference. The specific methods are as follows:

As is generally known, the following methods are used:

Modelname::model ()-> find ()//Finds an Object
Modelname::model ()-> findall ()///Find an array of object collections

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

$criteria = new Cdbcriteria;
$criteria->select = ' username,id,email ';
$criteria->order = ' id DESC ';
$users = Modelname::model ()->findall ($criteria);

Backstage accidentally see others have this writing, the method is very good:

$users = Modelname::model ()->findall (Array (
  ' select ' =>array (' username ', ' id ', ' email '),
  ' order ' = > ' id DESC ', 
));

After the test found that can be used, then find can also do so:

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

Of course, it's certainly not safe to do this, and replace it with 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 '), c11/>));

The same is possible with findall test.

Conclusion:

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

It is hoped that this article will help us to use YII for database programming.

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.