In this paper, the example shows that Yii uses find FindAll to locate the specified field and share it for everyone's reference. The specific methods are as follows:
As is generally known, the following methods are used:
Modelname::model ()-> find ()//Finds an Object
Modelname::model ()-> findall ()///Find an array of object collections
To find an array of objects and objects, how do I find the data for the fields I need, not the data for all the fields? That's what I did before:
$criteria = new Cdbcriteria;
$criteria->select = ' username,id,email ';
$criteria->order = ' id DESC ';
$users = Modelname::model ()->findall ($criteria);
Backstage accidentally see others have this writing, the method is very good:
$users = Modelname::model ()->findall (Array (
' select ' =>array (' username ', ' id ', ' email '),
' order ' = > ' id DESC ',
));
After the test found that can be used, then find can also do so:
$user = Modelname::model ()->find (Array ('
select ' =>array (' username ', ' id ', ' email '),
' order ' => ' ID DESC ',
' condition ' => ' id= '. $id,
));
Of course, it's certainly not safe to do this, and replace it with 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 '), c11/>));
The same is possible with findall test.
Conclusion:
This method can be very convenient to obtain the required data, of course, the need for paging time or need to new Cdbcriteria.
It is hoped that this article will help us to use YII for database programming.