Generally, the join relationship includes the following three types:
◇ One-to-one association: ONE_TO_ONE, including HAS_ONE and BELONGS_TO
◇ One-to-Multiple Association: ONE_TO_MANY, including HAS_MANY and BELONGS_TO
◇ Many-to-many Association: MANY_TO_MANY
Association Definition
The join CURD operation of a data table currently supports the following four types of associations: HAS_ONE, BELONGS_TO, has_detail, and many_to_detail.
A model can define multiple associations at the same time based on the complexity of the business model without restrictions. All Association definitions are defined in the $ _ link member variables of the model class, dynamic definition is also supported. To support association operations, the model class must inherit the RelationModel class. The format of association definition is:Copy codeThe Code is as follows: protected $ _ link = array (
'Join 1' => array (
'Association attribute 1' => 'define ',
'Correlated property n' => 'define ',
),
'Join 2' => array (
'Association attribute 1' => 'define ',
'Correlated property n' => 'define ',
),
...
);
HAS_ONE join method definition:Copy codeThe Code is as follows: class UserModel extends RelationModel
{
Public $ _ link = array (
'Profile '=> array (
'Ing ing _ type' => HAS_ONE,
'Class _ name' => 'profile ',
// Define more association attributes
......
),
);
}
Mapping_type indicates the association type, which must be defined by the HAS_ONE constant in the HAS_ONE Association.
Class_name: name of the model class to be associated
The ing_name associated ing name, used to obtain data
Name of the foreign key associated with foreign_key
Condition Association condition
Mapping_fields
As_fields maps the associated field values directly to a field in the data object.
BELONGS_TO join method definition:Copy codeThe Code is as follows: 'dept' => array (
'Ing ing _ type' => BELONGS_TO,
'Class _ name' => 'dept ',
'Foreign _ key' => 'userid ',
'Ing ing _ name' => 'dept ',
// Define more association attributes
......
),
Class_name: name of the model class to be associated
The ing_name associated ing name, used to obtain data
Name of the foreign key associated with foreign_key
Mapping_fields
Condition Association condition
Parent_key self-reference associated fields
As_fields maps the associated field values directly to a field in the data object.
HAS_MANY join method definition:Copy codeThe Code is as follows: 'Article' => array (
'Ing ing _ type' => has_ing,
'Class _ name' => 'Article ',
'Foreign _ key' => 'userid ',
'Ing ing _ name' => 'articles ',
'Ing ing _ order' => 'create _ time desc ',
// Define more association attributes
......
),
Class_name: name of the model class to be associated
The ing_name associated ing name, used to obtain data
Name of the foreign key associated with foreign_key
Parent_key self-reference associated fields
Condition Association condition
Mapping_fields
Mapping_limit join the number of records to be returned
Sorting of mapping_order join queries
MANY_TO_MANY join method definition:Copy codeThe Code is as follows: "Group" => array (
'Ing ing _ type' => MANY_TO_MANY,
'Class _ name' => 'group ',
'Ing ing _ name' => 'groupup ',
'Foreign _ key' => 'userid ',
'Relation _ foreign_key '=> 'goupid ',
'Relation _ table' => 'think _ gourpUser'
)
Class_name: name of the model class to be associated
The ing_name associated ing name, used to obtain data
Name of the foreign key associated with foreign_key
Relation_foreign_key: name of the foreign key associated with the table
Mapping_limit join the number of records to be returned
Sorting of mapping_order join queries
Relation_table multiple-to-many intermediate join table name
Join Query
Using the relation method for association operations, the relation method can not only enable association but also control local association operations, so that all associated operations can be controlled.
$ User = D ("User ");
$ User = $ User-> realtion (true)-> find (1 );
The output result of $ user may be similar to the following data:Copy codeThe Code is as follows: array (
'Id' => 1,
'Account' => 'thinkphp ',
'Password' => '123 ',
'Profile '=> array (
'Email '=> 'liu21st @ gmail.com ',
'Nickname' => 'Year ',
),
)
Join writeCopy codeThe Code is as follows: $ User = D ("User ");
$ Data = array ();
$ Data ["account"] = "ThinkPHP ";
$ Data ["password"] = "123456 ";
$ Data ["Profile"] = array (
'Email '=> 'liu21st @ gmail.com ',
'Nickname' => 'Year ',
);
$ Result = $ User-> relation (true)-> add ($ user );
In this way, the associated Profile data is automatically written.
Associated updateCopy codeThe Code is as follows: $ User = D ("User ");
$ Data ["account"] = "ThinkPHP ";
$ Data ["password"] = "123456 ";
$ Data ["Profile"] = array (
'Email '=> 'liu21st @ gmail.com ',
'Nickname' => 'Year ',
);
$ Result = $ User-> relation (true)-> where ('Id = 3')-> save ($ data );
Delete Association
$ Result = $ User-> relation (true)-> delete ("3 ");