Yii uses findfindAll to find the implementation method of the specified field. yiifindall_PHP tutorial

Source: Internet
Author: User
Yii uses findfindAll to find the implementation method of the specified field, yiifindall. Yii uses findfindAll to find the implementation method of the specified field. yiifindall this article shows the implementation method of Yii using findfindAll to find the specified field in the form of an instance, I would like to share with you how Yii can use find findAll to find the implementation method of the specified field. yiifindall

This article demonstrates how Yii uses find findAll to find the specified field in the form of an instance and shares it with you for your reference. The specific method is as follows:

The General Office adopts the following methods:

ModelName: model ()-> find () // identifies an object modelName: model ()-> findALL () // identifies an array of an object set

We can find the array of objects and object sets. how can we find the data of all fields instead of the data of all fields? Previously I did this:

$criteria = new CDbCriteria;$criteria->select = 'username,id,email';$criteria->order = 'id DESC';$users = modelName::model()->findAll( $criteria );

When someone else accidentally writes this in the background, the method is quite good:

$users = modelName::model()->findAll(array(  'select' =>array('username','id','email'),  'order' => 'id DESC', ));

After the test, it is found that it can be used, so find can also perform this operation:

$user = modelName::model()->find(array(  'select' =>array('username','id','email'),  'order' => 'id DESC',  'condition' => 'id='.$id,));

Of course, this operation is definitely not safe. you can use 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, you can use findAll for testing.

Conclusion:

In this way, you can easily obtain the required data. of course, you still need to create CDbCriteria when paging is required.

I hope this article will help you design database programs using Yii.


In c # is the use of Find and FindAll different?

Below are some standard methods for getting the data you need in the Model: findAll string $ conditions array $ fields string $ order int $ limit int $ page int $ recursive [$ conditions Type: string] search condition, which is the where clause in SQL, just like this $ conditions = "race = 'Wookie' AND thermal_detonators> 3 ". [$ Fields Type: array] the search attribute is projection, which specifies all the attributes you want to return. Here, I didn't use the name of the parent SQL field but the name of the parent object of attribute. I believe many PHPer are more familiar with the development mode based on SQL and DTO, however, the original intention of introducing the Model is to convert the relational database into an object operation. Therefore, the projection query should be understood as the retrieval of certain attributes of the object.) [$ order Type: string] the sorting attribute specifies the oder by attribute name [TODO: check whether the multiple order field be supported] [$ limit Type: int] result set data entry limit [$ page Type: int] the index of the result set by page. the default value is [$ recursive Type: int]. when the recursion is set to an integer greater than 1, other objects associated with the model are returned. If you have used an ORM tool like Hibernate, it is not hard to understand that this property is the LazyLoad attribute, but it is not completely equivalent. The value of a number represents the number of levels of cascading queries, for example, user. country. city. address. id) find string $ conditions array $ fields string $ order int $ recursive find method and findAll method are different in that the findAll method returns all matching result sets, the find method returns only the first result in the list.
 

Under what circumstances can Yii directly use find ()-> attributes to obtain the queried field?

This attribute must be defined in the object file corresponding to the database.
 

When findAll finds out the implementation method of the specified field, yiifindall this article shows the implementation method of Yii using find findAll to find the specified field in the form of an instance, and shares it with you for greater use...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.