Example analysis of ThinkPHP association model operations

  • 2020-05-26 07:56:02
  • OfStack

There are three types of associations:

◇ 1-to-1 associations: ONE_TO_ONE, including HAS_ONE and BELONGS_TO
◇ 1-to-many associations: ONE_TO_MANY, including HAS_MANY and BELONGS_TO
◇ many-to-many associations: MANY_TO_MANY

Associated definition

The association CURD operations for data tables are currently supported for four types of association: HAS_ONE, BELONGS_TO, HAS_MANY, MANY_TO_MANY.

A model can define multiple associations at the same time, depending on the complexity of the business model. All association definitions are defined in the $_link member variable of the model class and can be dynamically defined. To support association operations, the model class must inherit from the RelationModel class, which is defined in the following format:
 
protected $_link = array( 
'  associated  1' => array( 
'  The attribute  1' => '  define  ', 
'  The attribute  N' => '  define  ', 
), 
'  associated  2' => array( 
'  The attribute  1' => '  define  ', 
'  The attribute  N' => '  define  ', 
), 
... 
); 

Definition of HAS_ONE association mode:
 
class UserModel extends RelationModel 
{ 
public $_link = array( 
'Profile'=> array( 
'mapping_type' =>HAS_ONE, 
'class_name'=>'Profile', 
//  Define more associated properties  
 ...  
) , 
); 
} 

The mapping_type association type, which must be defined using the HAS_ONE constant in the HAS_ONE association.
The model class name to be associated with class_name
The mapping name associated with mapping_name to retrieve the data
The name of the foreign key associated with foreign_key
condition association condition
mapping_fields is associated with the field to query
as_fields maps the associated field values directly to a field in the data object

Definition of BELONGS_TO association mode:
 
'Dept'=> array( 
'mapping_type'=>BELONGS_TO, 
'class_name'=>'Dept', 
'foreign_key'=>'userId', 
'mapping_name'=>'dept', 
//  Define more associated properties  
 ...  
) , 

The model class name to be associated with class_name
The name of the mapping associated with mapping_name to retrieve the data
The name of the foreign key associated with foreign_key
mapping_fields is associated with the field to query
condition association condition
parent_key self-references the associated field of the association
as_fields maps the associated field values directly to a field in the data object

Definition of HAS_MANY association 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 model class name to associate
The mapping name associated with mapping_name for data retrieval
The name of the foreign key associated with foreign_key
parent_key self-references the associated field of the association
condition association condition
mapping_fields is associated with the field to query
mapping_limit is associated with the number of records to return
Sorting of mapping_order association queries

Definition of MANY_TO_MANY association 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' 
) 

The model class name to be associated with class_name
The name of the mapping associated with mapping_name for data retrieval
The name of the foreign key associated with foreign_key
relation_foreign_key associates the foreign key name of the table
mapping_limit is associated with the number of records to return
Sorting of mapping_order association queries
relation_table many-to-many intermediate associated table name

Associated query

Using relation method for association operation, relation method can not only enable the association but also control the local association operation, so the association operation 1 is fully under control.

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

The output of $user might result in data similar to the following:
 
array( 
'id'=>1, 
'account'=>'ThinkPHP', 
'password'=>'123456', 
'Profile'=> array( 
'email'=>'liu21st@gmail.com', 
'nickname'=>' Time flies ', 
) , 
) 

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

This automatically writes the associated Profile data.

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

Link to delete

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

Related articles: