Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加多租户模型 #6722

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/database/src/Model/TenantField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact wfuren@qq.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Hyperf\Database\Model;

use Exception;
use Hyperf\DbConnection\Model\Model;

/**
* 单表字段多租户版。
* 使用方法:一般你的orm会extends一个Model,在Model文件中use即可(如你只有部分orm需要,则不要在Model中use,只需要在指定orm中use即可。默认字段名为“tenant_id”,如要修改可在orm中添加成员变量“tenant”即可。)
* @method static Model tenant(array|string|null $tenantId) 注意如果为null代表不增加租户限制条件。
*/
trait TenantField
{
public static function bootTenantField(): void
{
static::addGlobalScope(new TenantFieldScope()); // 全局作用域
}

/**
* 添加一个链式tenant方法(注:这里操作的方式是全局,如修改代码请注意)
*/
public static function scopeTenant(Builder $builder, array|string|null $_tenantId): Builder
{
/*为null代表删除全局租户条件*/
if ($_tenantId === null) {
return $builder->withoutGlobalScope(TenantFieldScope::class);
}

$tenantId = is_array($_tenantId) ? $_tenantId : [$_tenantId];

return $builder->withoutGlobalScope(TenantFieldScope::class)->withGlobalScope(
TenantFieldScope::class,
function (Builder $builder) use ($tenantId) {
/** @noinspection PhpUndefinedMethodInspection */
$builder->whereIn($builder->getModel()->getQualifiedTenantIdColumn(), $tenantId);
}
);
}

/**
* 获取带数据表的租户字段。
*
* @return string
*/
public function getQualifiedTenantIdColumn(): string
{
$column = 'tenant_id';
if (property_exists($this, 'tenant')) {
$column = $this->tenant;
}
return $column;
}

/**
* 获取租户id的值
*
* @return array|null
* @throws Exception
*/
public function getTenantIdFun(): array|null
{
$column = 'tenantIdCallback';
if (property_exists($this, $column)) {
return $this->{$this->{$column}}();
}
if ( ! method_exists($this, 'getTenantIdVal')) {
throw new Exception('请在模型引用处手动实现getTenantIdVal方法,自行组装业务逻辑,也可通过成员变量tenantIdCallback来指定方法名。');
}
return $this->getTenantIdVal();
}
}
29 changes: 29 additions & 0 deletions src/database/src/Model/TenantFieldScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact wfuren@qq.com
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace Hyperf\Database\Model;

class TenantFieldScope implements Scope
{
public function apply(Builder $builder, Model $model): Builder
{
/**
* @var TenantTrait $model 这里的class类其实就是调用的Dao层模型。
*/
/** @noinspection PhpUnhandledExceptionInspection */
$tenantId = $model->getTenantIdFun();
if ($model->getQualifiedTenantIdColumn() === null || $tenantId === null) {
return $builder;
}
return $builder->whereIn($model->getQualifiedTenantIdColumn(), $tenantId);
}
}