Home  >  Article  >  Backend Development  >  ThinkPHP association model operation example analysis_PHP tutorial

ThinkPHP association model operation example analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:16:31890browse

Usually, the associations we talk about include the following three types:

◇ One-to-one association: ONE_TO_ONE, including HAS_ONE and BELONGS_TO
◇ One-to-many association: ONE_TO_MANY, including HAS_MANY and BELONGS_TO
◇ Many For many associations: MANY_TO_MANY

Association definition

Associative CURD operation of the data table. The currently supported associations include the following four types: HAS_ONE, BELONGS_TO, HAS_MANY, MANY_TO_MANY.

A model can define multiple associations at the same time according to the complexity of the business model without restriction. All association definitions 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. The format of the association definition is:

Copy the code The code is as follows:

protected $_link = array(
' Association 1' => array(
' Association attribute 1' => ' Definition',
' Association attribute N' => ' Definition',
),
' Association 2' => array(
' Association attribute 1' => ' Definition',
' Association attribute N' => ' Definition',
),
...
);

HAS_ONE association method definition:
Copy code The code is as follows:

class UserModel extends RelationModel
{
public $_link = array(
'Profile'=> array(
'mapping_type' =>HAS_ONE,
'class_name'=>'Profile',
// Define more associated attributes
...
) ,
);
}

mapping_type Association type, this must be defined using the HAS_ONE constant in the HAS_ONE association.
class_name The name of the model class to be associated
mapping_name The associated mapping name, used to obtain data
foreign_key The associated foreign key name
condition The associated condition
mapping_fields The associated fields to be queried
as_fields directly maps the associated field value to a field in the data object

BELONGS_TO Definition of association method:
Copy code Code As follows:

'Dept'=> array(
'mapping_type'=>BELONGS_TO,
'class_name'=>'Dept',
'foreign_key'= >'userId',
'mapping_name'=>'dept',
// Define more associated attributes
...
) ,

class_name Model class name to be associated
mapping_name associated mapping name, used to obtain data
foreign_key associated foreign key name
mapping_fields associated field to be queried
condition associated condition
parent_key self-reference The associated associated fields
as_fields directly map the associated field value to a certain field in the data object

HAS_MANY Definition of association method:
Copy code The code is as follows:

'Article'=> array(
'mapping_type' =>HAS_MANY,
'class_name'=>'Article',
'foreign_key'=>'userId',
'mapping_name'=>'articles',
'mapping_order'=>'create_time desc',
// Define more associated attributes
......
) ,

class_name The model class name to be associated
mapping_name The associated mapping name, used to obtain data
foreign_key The associated foreign key name
parent_key Self-referential association related fields
condition Association conditions
mapping_fields Fields to be queried for association
mapping_limit Number of records to be returned for association
mapping_order Order of association query

MANY_TO_MANY association Definition of method:
Copy code The code is as follows:

"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 model class name to be associated
mapping_name The associated mapping name, used to obtain data
foreign_key The associated foreign key name
relation_foreign_key The foreign key name of the associated table
mapping_limit The association to return The number of records
mapping_order Sorting of relational query
relation_table Many-to-many intermediate relation table name

Relationship query

Use the relation method to perform correlation operations. The relation method can not only enable correlation You can also control local association operations, so that all association operations are under control.

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

Output $user result possible It is data similar to the following:
Copy code The code is as follows:

array(
'id'=> 1,
'account'=>'ThinkPHP',
'password'=>'123456',
'Profile'=> array(
'email'=>'liu21st @gmail.com',
'nickname'=>'Fleeting',
) ,
)

Associated writing
Copy code The code is as follows:

$User = D( "User" );
$data = array();
$data["account "]="ThinkPHP";
$data["password"]="123456";
$data["Profile"]=array(
'email'=>'liu21st@gmail.com ',
'nickname' =>' Fleeting Years',
) ;
$result = $User->relation(true)->add($user);

This will automatically write the associated Profile data.

Association update
Copy code The code is as follows:

$User = D( "User" );
$data["account"]= "ThinkPHP";
$data["password"]= "123456";
$data["Profile"]=array(
'email'= >'liu21st@gmail.com',
'nickname' =>'流年',
) ;
$result =$User-> relation(true)->where('id =3')->save($data);

Relation Delete

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325937.htmlTechArticleUsually the association relationships we talk about include the following three types: ◇ One-to-one association: ONE_TO_ONE, including HAS_ONE and BELONGS_TO ◇ One-to-many association: ONE_TO_MANY, including HAS_MANY and...
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