The correlation model of thinkphp

Source: Internet
Author: User
thinkphp Correlation Model Operation example analysis, the need for friends can refer to the next

In general, we call the following three types of relationships:
◇ one-to-one links: One_to_one, including Has_one and belongs_to
◇ one-to-many associations: One_to_many, including Has_many and belongs_to
◇ Many-to-many associations: Many_to_many
Association Definitions
The associated curd operation of the data table, the currently supported correlation includes the following four types: Has_one, belongs_to, Has_many, Many_to_many
A model can define multiple associations at the same time based on the complexity of the business model, all of which are defined in the $_link member variable of the model class, and can support dynamic definition. To support association operations, the model class must inherit the Relationmodel class, and the format of the association definition is:

Protected $_link = Array (' associative 1 ' = = Array (' association attribute 1 ' = ' = ' = ' definition ', ' association attribute N ' = ' definition ', '), ' association 2 ' = = Array (' Off The associated attribute 1 ' = = ' definition ', ' association attribute N ' = ' definition ', '), ...);

Has_one definition of the associated mode:

Class Usermodel extends Relationmodel {public $_link = array (' profile ' = = Array (' Mapping_type ' =>has_one, ' class_n Ame ' + ' profile ',//define more associated properties ...) , ); }

The Mapping_type association type, which must be defined using the Has_one constant in the Has_one association.
Class_name the name of the model class to associate
Mapping_name the name of the map associated with the data used to obtain the
Foreign_key name of the associated foreign key
Condition Association Conditions
Mapping_fields associating fields to query
As_fields directly maps the associated field values into a field in a data object
Belongs_to definition of the associated mode:

' Dept ' = = Array (' Mapping_type ' =>belongs_to, ' class_name ' = ' Dept ', ' foreign_key ' = ' userId ', ' mapping_ Name ' = ' dept ',//define more associated properties ...) ,

Class_name the name of the model class to associate
Mapping_name the name of the map associated with the data used to obtain the
Foreign_key name of the associated foreign key
Mapping_fields associating fields to query
Condition Association Conditions
Parent_key associated Fields of a self-referencing association
As_fields directly maps the associated field values into a field in a data object
Has_many definition of the associated mode:

' Article ' = = Array (' Mapping_type ' =>has_many, ' class_name ' = ' article ', ' foreign_key ' = ' userId ', ' mapping _name ' + ' articles ', ' mapping_order ' = ' create_time desc ',//define more associated properties ...) ,

Class_name the name of the model class to associate
Mapping_name the name of the map associated with the data used to obtain the
Foreign_key name of the associated foreign key
Parent_key associated Fields of a self-referencing association
Condition Association Conditions
Mapping_fields associating fields to query
Mapping_limit Association to return the number of records
Mapping_order sorting of associated queries
Many_to_many definition of the associated mode:

"Group" =>array (' Mapping_type ' =>many_to_many, ' class_name ' = ' group ', ' mapping_name ' = ' groups ', ' Foreign_key ' + ' userId ', ' relation_foreign_key ' = ' goupid ', ' relation_table ' = ' think_gourpuser ')

Class_name the name of the model class to associate
Mapping_name the name of the map associated with the data used to obtain the
Foreign_key name of the associated foreign key
Relation_foreign_key the foreign key name of the associated table
Mapping_limit Association to return the number of records
Mapping_order sorting of associated queries
Relation_table Many-to-many intermediate association table names
Correlation Query
Using the relation method to correlate operations, the relation method can not only enable correlation but also control the local association operation, so that the correlation operation can be mastered.

$User = D ("User"); $user = $User->realtion (True)->find (1);

The output $user results may be similar to the following data:

Array (' ID ' =>1, ' account ' = ' thinkphp ', ' password ' = ' 123456 ', ' profile ' = ' + ' array (' email ' = ') ' Liu21st@gmail.com ', ' nickname ' = ' fleeting ',),)

Association writes

$User = D ("User"); $data = Array (); $data ["Account"]= "thinkphp"; $data ["Password"]= "123456"; $data ["Profile"]=array (' email ' = ' liu21st@gmail.com ', ' nickname ' = ' fleeting '); $result = $User->relation (True)->add ($user);

This will automatically write the associated profile data.
Associated Updates

$User = D ("User"); $data ["Account"]= "thinkphp"; $data ["Password"]= "123456"; $data ["Profile"]=array (' email ' = ' liu21st@gmail.com ', ' nickname ' = ' fleeting '); $result = $User Relation (true)->where (' id=3 ')->save ($data);

Associate Delete

$result = $User->relation (true)->delete ("3");

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.