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';
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); }
$ob->attributes['title'];
$count =VideoInfo::model()->count($criteria) $pages=new CPagination($count); $pages->pageSize=30; //每页分多少条 $pages->applyLimit($criteria);
$result = VideoInfo::model()->findAll($criteria);