phariscope/multitenant

多租户组件。

安装: 26

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 0

开放问题: 0

类型:symfony-bundle

0.0.2 2024-09-19 13:45 UTC

This package is auto-updated.

Last update: 2024-09-20 10:08:26 UTC


README

无需(太多)修改代码,轻松将多租户功能添加到您的Symfony项目中。

安装

使用Composer安装此包

composer require phariscope/multitenant

您可以将Multitenant用作一个Symfony包。只需在您的config/bundles.php文件中添加一行即可

return [
    // other bundles

    Phariscope\MultiTenant\MultiTenantBundle::class => ['all' => true],
];

用法

在Symfony控制器中,按照以下步骤操作

  1. EntityManagerResolver注入到控制器构造函数中。
  2. 在您的路由操作中检索特定租户的实体管理器。
  3. 如果该租户不存在数据库,则创建租户数据库和模式
  4. 享受...

例如,假设您的请求或会话中有一个tenant_id

class YourController extends AbstractController
{
    public function __construct(
        private EntityManagerResolver $entityManagerResolver,
    ) {}

    #[Route('your/route', name: 'runYourRoute', methods: ['POST', 'GET'])]
    public function runYourRoute(Request $request): Response
    {
        $tenantEntityManager = $this->entityManagerResolver->getEntityManagerByRequest($request);
        (new DatabaseTools())->createDatabaseIfNotExists($entityManager);

        $repository = new YourSomeEntityDoctrineRepository($tenantEntityManager);

        // Your code here...
    }
}

创建租户数据库

确保您已设置必要的控制台来处理租户操作。

要为特定租户(例如,tenantID1234)创建数据库,您可以使用控制台命令

bin/console tenant:database:create tenantID1234

为租户数据库创建模式

一旦您创建了一个租户数据库,您就可以创建它的模式。

您可以使用控制台命令

bin/console tenant:schema:create tenantID1234