[ThinkPHP6.x framework] (11) association model of database

For association methods, the system provides 9 schemes, as follows:

In the above example, we use the one-to-one association model, which also has relative reverse association;

An example is given below:

class Profile extends Model {
    public function user() {
    return $this->belongsTo(User::class); 
    }
}
$profile = ProfileModel::find(1); 
return $profile->user->email;

One to one association query

One {tp_user table, the primary key is: ID; Attached table: tp_profile, create two fields: user_id and} hobby, and the foreign key is user_id.

hasOne

hasOne mode is suitable for the main table and associated tables. The specific setting methods are as follows:

hasOne('association model ',' foreign key ',' primary key '];

Association model (required): the associated model name or class name
Foreign key: the default foreign key rule is the current model name (excluding namespace, the same below)+_ ID, such as user_id
Primary key: the primary key of the current model. It will be obtained automatically by default. You can also specify the incoming key

return $this->hasOne(Profile::class,'user_id', 'id');

Create User model and Profile model, both of which are empty models; On the User model side, you need to associate profiles. The specific methods are as follows:

class User extends Model
{
    public function profile() {
    //hasOne indicates one-to-one association, parameter 1 indicates attached table, parameter 2 external key, default user_id 
    return $this->hasOne(Profile::class,'user_id');
} }

Create a controller to test the output: grade php:

$user = UserModel::find(21);
return json($user->profile); 
return $user->profile->hobby;

Use the save() method to set association modification and modify the value of the attached table field through the main table;

$user = UserModel::find(19);
$user->profile->save(['hobby'=>'Love miss']);

- > data can be modified by the profile attribute method, and data can be added by the > profile () method;

$user->profile()->save(['hobby'=>'I don't like green pepper']);

belongsTo

belongsTo mode is applicable to the main table associated with the attached table. The specific setting method is as follows:

belongsTo('association model ',' foreign key ',' Association primary key '];

return $this->belongsTo(Profile::class,'user_id', 'id');

Association model (required): model name or model class name
Foreign key: the foreign key of the current model. The default foreign key name rule is the associated model name+_ id
Associated primary key: associated model primary key, which is usually automatically obtained or specified

The query scheme for {belongsTo() is as follows:

$profile = ProfileModel::find(1);
return $profile->user->email;

Using {hasOne() can also simulate} belongsTo() to query;

//Parameter 1 represents the profile method of the User model class, not the profile model class 
$user = UserModel::hasWhere('profile', ['id'=>2])->find(); 
return json($user);

//Closure is adopted. Here are two table operations, which will lead to fuzzy id recognition. It is necessary to specify the table 
$user = UserModel::hasWhere('profile', function ($query) {
    $query->where('profile.id', 2); 
})->select();
return json($user);

One to many association query

hasMany mode

The hasMany mode is suitable for the main table and associated tables to realize one to many query. The specific setting methods are as follows:

hasMany('association model ',' foreign key ',' primary key '];

return $this->hasMany(Profile::class,'user_id', 'id');

Association model (required): model name or model class name
Foreign key: associated model foreign key. The default foreign key name rule is the current model name+_ id
Primary key: the primary key of the current model. It is usually obtained automatically or specified to be passed in

Use the - > profile () method pattern to further filter the data;

$user->profile()->where('id', '>', 10)->select();
$user->profile->where('id', '>', 10)

Use the ^ has() method to query the main table contents of the associated attached table, such as the main table records with ^ 2 or more records;

UserModel::has('profile', '>=', 2)->select();

Use the # hasWhere() method to query the filtered records of associated schedules, such as the main table records that have passed the interest approval;

UserModel::hasWhere('profile', ['status'=>1])->select();

Use save() and save all() to add Association and batch Association, as follows:

$user = UserModel::find(19);
$user->profile()->save(['hobby'=>'Test preferences', 'status'=>1]); 
$user->profile()->saveAll([
    ['hobby'=>'Test preferences', 'status'=>1], 
    ['hobby'=>'Test preferences', 'status'=>1]
]);

Use the {together() method to delete all the contents associated with the attached table when deleting the contents of the main table;

$user = UserModel::with('profile')->find(227);
$user->together(['profile'])->delete();

Keywords: Back-end

Added by 10legit10quit on Sat, 15 Jan 2022 20:59:42 +0200