Yii uses find FindAll to look up the implementation of the specified field, very practical skills, the needs of friends can refer to the next.
Friends who have used Yii know that the following methods are used:
View Code Printing
1 |
modelName::model()->find() //找出的是一个对象 |
2 |
modelName::model()->findALL() //找出的是一个对象集合的数组 |
You can find an array of objects and objects, so how do I find the data for the fields I need instead of the data for all the fields? That's what I did before:
View Code Printing
1 |
$criteria = new CDbCriteria; |
2 |
$criteria ->select= ‘username,id,email‘ ; |
3 |
$criteria ->order= ‘id DESC‘ ; |
4 |
$users =modelName::model()->findAll( $criteria ); |
Backstage to see others have so written, the method is very good:
View Code Printing
1 |
$users =modelName::model()->findAll( array ( ‘select‘ => array ( ‘username‘ , ‘id‘ , ‘email‘ ), ‘order‘ => ‘id DESC‘ )); |
After the test is found to work, then find can also do this:
View Code Printing
1 |
$user =modelName::model()->find( array ( ‘select‘ => array ( ‘username‘ , ‘id‘ , ‘email‘ ), ‘order‘ => ‘id DESC‘ , ‘condition‘ => ‘id=‘ . $id , )); |
Sure, it's not safe to do this, but the following method can also be used:
View Code Printing
1 |
$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‘
)));
|
It is also possible to test with FindAll.
Conclusion:
This method can be very convenient to obtain the required data, of course, the need for paging or new Cdbcriteria.
Yii uses find FindAll to find out how to implement a specified field