is generally known
modelName::model() -> find() <span>//</span><span>找出的是一个对象</span> modelName::model() -> findALL() <span>//</span><span>找出的是一个对象集合的数组</span>
How to find out the data of the fields I need instead of all fields
I did this before
<span>$criteria</span> = <span>new</span><span> CDbCriteria; </span><span>$criteria</span>->select = 'username,id,email'<span>; </span><span>$criteria</span>->order = 'id DESC'<span>; </span><span>$users</span> = modelName::model()->findAll( <span>$criteria</span> );
I accidentally saw someone else writing something like this backstage and realized how ignorant I was
<span>$users</span> = modelName::model()->findAll(<span>array</span><span>( </span>'select' =><span>array</span>('username','id','email'), 'order' => 'id DESC',<span> ));</span>
After testing, I found that it works, so find can also do the same thing
<span>$user</span> = modelName::model()->find(<span>array</span><span>( </span>'select' =><span>array</span>('username','id','email'), 'order' => 'id DESC', 'condition' => 'id='.$id,<span> ));</span>
Of course, it is definitely not safe to do this. You can also use the following method
<span>$users</span> = <span>$this</span>->user->find(<span>array</span><span>( </span>'select'=><span>array</span>('id','username','email'), 'order' => 'id DESC', 'condition' => 'state=:state AND id=:id', 'params' => <span>array</span>(':state'=>'1',':id' => '2'),<span> ));</span>
Similarly, it can be tested with findAll, conclusion
This method can easily obtain the required data. Of course, when pagination is required, you still need to new the following CDbCriteria
$retrievedProject=Project::model()->findall();
foreach($retrievedProject as $v){
echo $v->attributes['title'];
}
Write like this:
$db = new CDbCriteria();
$db->addInCondition('id', array(1,2,3));
$newstypelist=NewsType:: model()->findAll($db);