Find FindAll found in Yii Framework comparison of methods for establishing fields _php instance

Source: Internet
Author: User
Tags yii

Known

Modelname::model ()-> find ()//Finding an Object
Modelname::model ()-> findall ()///Find an array of object collections
How to 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 so written, find themselves how ignorant

$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 operate this

$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 '),
));

It's also possible to use the FindAll test, the conclusion

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

For example, I want to remove the ' v_id ', ' title ', ' Big_class ', ' sub_class ', ' upload_time ', ' comment_num ', and so on in the Videoinfo table, and the conditions are Status=1, Follow LastModifyTime in reverse order, and only 3 can be removed to do so:

$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); 

One of the lines I commented out is a variable, with a placeholder, for example, if your status needs to be assigned according to the variable, you can assign a value to the line in the annotation, and then write the condition condition

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

Can
So, the result you get when you $result a variable, it's a list of objects that you need to traverse:

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

Like you want to show each field just by typing

 
 

Wait, you can.

Cpagination class can be used with the Cdbcriteria class and the front page plugin to support paging:

      $count =videoinfo::model ()->count ($criteria)

      $pages =new cpagination ($count);    
      $pages->pagesize=30; How many
      $pages->applylimit per page ($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.