Comparison of find and findAll Methods for Finding Formulated Fields in Yii Framework

  • 2021-07-18 07:15:11
  • OfStack

As is known to all

modelName::model() - > find ()//Find 1 object
modelName::model() - > findALL ()//Find an array of 1 object collection
How do I find that data for the field 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 inadvertently saw someone else have such a write, found themselves how ignorant


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

After testing, it is found that it can be used, so find can also operate like this


$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. It is also possible to change to the following methods


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

In the same way, it can be tested with findAll. Conclusion

Through this method, you can easily obtain the required data. Of course, when you need paging, you still need new below CDbCriteria

For example, I want to take out the fields such as' v_id ',' title ',' big_class ',' sub_class ',' upload_time ',' comment_num 'in the videoinfo table, and the condition is that status=1, in reverse order according to lastmodifytime, and only three can be taken out


$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 variables ) ; 
$result = VideoInfo::model()->findAll($criteria); 

Among them, the 1 line I commented out can pass variables, which is represented by placeholders. For example, if your status needs to be conditionally assigned according to variables, you can assign values in the annotated line, and then write them conditionally in condition


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

That's enough,
In this way, the result you get with the $result variable is a list of objects, which needs to be traversed 1 time:


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

For example, if you want to show each field, just type it


$ob->attributes['title']; 

Just wait

After that, the CPagination class can be used to support paging together with the CDbCriteria class and the foreground paging plug-in 1:


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

      $pages=new CPagination($count);    
      $pages->pageSize=30; // How many articles are divided into each page 
      $pages->applyLimit($criteria); 


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


Related articles: