Home > Article > Backend Development > Comparison of methods for finding specified fields using findfindAll in the Yii framework
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 );
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', ));
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, ));
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'), ));
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);
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';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); }For example, if you want to display each field, just type
$ob->attributes['title'];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);
$result = VideoInfo::model()->findAll($criteria);For more comparisons of methods of findfindAll to find specified fields in the Yii framework, please pay attention to the PHP Chinese website!