Home >Backend Development >PHP Tutorial >Yii Regarding the method of find findAll to find the specified fields, yiifindall_PHP tutorial

Yii Regarding the method of find findAll to find the specified fields, yiifindall_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:23782browse

Yii Regarding the method of find findAll to find the specified fields, yiifindall

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

yii framework, $retrievedProject=Project ::model()->findall(); How to output?

$retrievedProject=Project::model()->findall();
foreach($retrievedProject as $v){
echo $v->attributes['title'];
}

yii query statement writing method

Write like this:
$db = new CDbCriteria();
$db->addInCondition('id', array(1,2,3));
$newstypelist=NewsType:: model()->findAll($db);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/874529.htmlTechArticleYii Regarding find findAll method of finding specified fields, yiifindall is generally known modelName::model() - find () // What is found is an object modelName::model() - findALL() // What is found...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn