Home > Backend Development > PHP Tutorial > Comparison of methods for finding specified fields using findfindAll in the Yii framework

Comparison of methods for finding specified fields using findfindAll in the Yii framework

高洛峰
Release: 2023-03-05 08:00:01
Original
956 people have browsed it

As we all know

modelName::model() -> find()  //What is found is an object
modelName::model() -> findALL()  //What is found It is an array of object collections
How to find the data of the fields I need, not the data of all fields

I did this before

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

Backstage I accidentally saw someone else writing something like this, and realized how ignorant I was

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

After testing, I found that it really works, so find can also operate like this

$user = modelName::model()->find(array(
  'select' =>array('username','id','email'),
  'order' => 'id DESC',
  'condition' => 'id='.$id,
));
Copy after login

Of course, it is definitely not safe to do this. It can be replaced by 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'),
));
Copy after login

Similarly, it can also be tested with findAll. Conclusion

This method can easily obtain the required data. Of course, when paging is required, new is required for the following CDbCriteria.

For example, I want to take out the 'v_id', 'title' in the videoinfo table, 'big_class', 'sub_class', 'upload_time', 'comment_num' and other fields, and the condition is status=1, follow lastmodifytime in reverse order, and only take out 3, do this:

$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' => $你的变量) ;
$result = VideoInfo::model()->findAll($criteria);
Copy after login

The line I commented out can be used to pass variables, represented by placeholders. For example, if your status needs to be conditionally assigned according to variables, you can assign a value in the commented line, and then write ## in the condition condition. #

$criteria -> condition = 'status = :status';
Copy after login

That’s it,

In this way, the $result variable is the result you obtained. It is a list of objects and needs to be traversed:

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

For example, if you want to display each field, just type

$ob->attributes['title'];
Copy after login

and so on.

is finished. The CPagination class can be used together with the CDbCriteria class and the front-end paging plug-in. Supports paging:

$count =VideoInfo::model()->count($criteria)
 
$pages=new CPagination($count);   
$pages->pageSize=30; //每页分多少条
$pages->applyLimit($criteria);
Copy after login
$result = VideoInfo::model()->findAll($criteria);
Copy after login

For more comparisons of methods of findfindAll to find specified fields in the Yii framework, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template