Yii Use find findAll to find the implementation of the specified field
This article mainly introduces the implementation of Yii using find findAll to find the specified field. It is a very practical technique. For more information, see.
Anyone who has used Yii knows that the following method is used:
ModelName: model ()-> find () // identifies an object modelName: model ()-> findALL () // identifies an array of an object set
We can find the array of objects and object sets. How can we find the data of all fields instead of the data of all fields? Previously I did this:
$criteria=new CDbCriteria;$criteria->select='username,id,email';$criteria->order='id DESC';$users=modelName::model()->findAll( $criteria );
When someone else accidentally writes this in the background, the method is quite good:
$users=modelName::model()->findAll(array('select'=>array('username','id','email'),'order'=>'id DESC'));
After the test, it is found that it can be used, so find can also perform this operation:
$user=modelName::model()->find(array('select'=>array('username','id','email'),'order'=>'id DESC','condition'=>'id='.$id, ));
Of course, this operation is definitely not safe. You can use 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, you can use findAll for testing.
Conclusion:
In this way, you can easily obtain the required data. Of course, you still need to create CDbCriteria when paging is required.
Articles you may be interested in
- Yii view (output) the SQL statement executed on the current page
- Module Development and Analysis of Yii framework
- Yii controller action parameter binding
- Yii database addition, modification, and deletion operations Summary
- Convert the result of yii object to an array
- Detailed analysis on the directory structure of the yii framework
- Yii framework cache knowledge Summary
- Yii database query Operation Summary