0
点赞
收藏
分享

微信扫一扫

Thinkphp6模型关联

莞尔小迷糊 2023-05-06 阅读 100

文章目录


前言

ThinkPHP 6 模型关联是指使用 PHP 对象关系映射(ORM)机制,通过模型类的关联属性,在两个或多个数据表之间建立关联关系。

以下是 ThinkPHP 6 中常用的模型关联方式:


提示:以下是本篇文章正文内容,下面案例可供参考

一、一对一关联

在一对一关联中,一个模型类对应于另一个模型类的唯一实例。例如一个用户只有一个详细信息,可以通过定义下面的关联方法来表示:
其中,User 模型类中的 profile() 方法返回一个 hasOne 实例,表示一个用户只有一个详细信息,而 Profile 模型类中的 user() 方法则返回一个 belongsTo 实例,表示一个详细信息属于一个用户。

use think\Model;
use app\common\model\User;
use app\common\model\Profile;

class User extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

class Profile extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

示例

<?php
namespace app\model;

use think\Model;

class UserModel extends Model
{
    // 设置模型对应的数据表名
    protected $table = 'user';

    // 定义用户和个人资料之间的一对一关联
    public function profile()
    {
    	//user_id:外键id
        return $this->hasOne(ProfileModel::class, 'user_id');
    }
}

//查询
use app\model\UserModel;

// 查询所有用户以及其对应的个人资料信息
$users = UserModel::with('profile')->select();

反向查询

namespace app\model;

use think\Model;

class ProfileModel extends Model
{
    // 设置模型对应的数据表名
    protected $table = 'profile';

    // 定义个人资料和用户之间的一对一反向关联
    public function user()
    {
    	//user_id:外键id
        return $this->belongsTo(UserModel::class, 'user_id');
    }
}

//查询
use app\model\ProfileModel;

// 查询所有个人资料以及对应的用户信息
$profiles = ProfileModel::with('user')->select();

二、一对多

在一对多关联中,一个模型类对应于另一个模型类的多个实例。例如一个班级有多个学生,可以通过定义下面的关联方法来表示:
其中,Clazz 模型类中的 students() 方法返回一个 hasMany 实例,表示一个班级有多个学生,而 Student 模型类中的 clazz() 方法则返回一个 belongsTo 实例,表示一个学生属于一个班级。

class Clazz extends Model
{
    public function students()
    {
        return $this->hasMany(Student::class);
    }
}

class Student extends Model
{
    public function clazz()
    {
        return $this->belongsTo(Clazz::class);
    }
}

三、多对多

在多对多关联中,多个模型类之间通过一个中间表建立关联关系。例如一个文章可以有多个标签,一个标签也可以对应多篇文章,可以通过定义下面的关联方法来表示:

其中,Article 模型类中的 tags() 方法返回一个 belongsToMany 实例,表示一篇文章可以有多个标签,而 Tag 模型类中的 articles() 方法则返回一个 belongsToMany 实例,表示一个标签可以对应多篇文章。

class Article extends Model
{
    public function tags()
    {
        return $this->belongsToMany(Tag::class);
    }
}

class Tag extends Model
{
    public function articles()
    {
        return $this->belongsToMany(Article::class);
    }
}

四、 示例

<?php
namespace app\model;

use think\Model;

class UserModel extends Model
{
    // 设置模型对应的数据表名
    protected $table = 'user';

    // 定义用户和个人资料之间的一对一关联
    public function profile()
    {
    	//user_id:外键id
        return $this->hasOne(ProfileModel::class, 'user_id');
    }
}

//查询
use app\model\UserModel;

// 查询所有用户以及其对应的个人资料信息
$users = UserModel::with('profile')->select();

反向查询

namespace app\model;

use think\Model;

class ProfileModel extends Model
{
    // 设置模型对应的数据表名
    protected $table = 'profile';

    // 定义个人资料和用户之间的一对一反向关联
    public function user()
    {
    	//user_id:外键id
        return $this->belongsTo(UserModel::class, 'user_id');
    }
}

//查询
use app\model\ProfileModel;

// 查询所有个人资料以及对应的用户信息
$profiles = ProfileModel::with('user')->select();

总结

hasMany

hasMany 方法用于定义一个「一对多」的关联关系,其参数形式为:

$this->hasMany($related, $foreignKey = null, $localKey = null)

belongsTo

belongsTo 方法用于定义一个「多对一」关联关系,其参数形式为:

$this->belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)

hasOne

hasOne 方法用于定义一个「一对一」关联关系,与 hasMany 不同的是,它指定的是一个模型拥有一个与另一个模型的关联数据,其参数形式为:

$this->hasOne($related, $foreignKey = null, $localKey = null)
举报

相关推荐

0 条评论