zaeder/multidb-bundle

Symfony MultiDbBundle

安装次数: 23

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

dev-master 2020-07-22 08:17 UTC

This package is auto-updated.

Last update: 2024-09-22 18:22:23 UTC


README

一个具有本地数据库和动态分布数据库的 symfony 4 扩展包

使用本地数据库进行应用程序配置,并为每个客户端使用单独的数据库

安装

composer require zaeder/multidb-bundle 

创建实体

您必须创建 3 个实体

  • 对于本地数据库
    • 服务器实现 Zaeder\MultiDbBundle\Entity\ServerInterface,具有 Zaeder\MultiDbBundle\Repository\ServerRepository 或其扩展类的存储库
    • 用户实现 Zaeder\MultiDbBundle\Entity\LocalUserInterface,具有 Zaeder\MultiDbBundle\Repository\LocalUserRepository 或其扩展类的存储库
  • 对于分布数据库
    • 用户实现 Zaeder\MultiDbBundle\Entity\DistUserInterface,具有 Zaeder\MultiDbBundle\Repository\DistUserRepository 或其扩展类的存储库

我们建议将实体和存储库分别放入 Local 和 Dist 文件夹中

配置

您需要在 doctrine 配置中定义两个连接和实体管理器。我们建议默认使用客户端数据库的连接,这会使自动注入更容易。使用与本地连接相同的配置作为默认值,它将在登录时重新配置

添加到您的配置中

multi_db:
  local:
    connection: localConnectionName
    entityManager: localEntityManagerName
    tablePrefix: '' #optional
    serverEntity: pathToServerEntity
    userEntity: pathToLocalUserEntity
  dist:
    connection: distConnectionName
    entityManager: distEntityManagerName
    tablePrefix: '' #optional
    userEntity: pathToDistUserEntity
  passwordKey: enterAnHash #better to define in .env or parameters.yml
  loginRedirect:
    - {role: 'roleName', route: 'routeName'} # Define higher role first

在 config/bundles.php 中添加扩展包定义

Zaeder\MultiDbBundle\MultiDbBundle::class => ['all' => true],

编辑 config/packages/security.yaml

 security:
   encoders:
     pathToLocalUserEntity:
       algorithm: bcrypt
   providers:
     users:
       entity: pathToLocalUserEntity
       property: 'username'
       manager_name: 'localEntityManagerName'
   firewalls:
     [...]
     local:
       pattern: ^/
       anonymous: true
       providers: users
       guard:
         authenticators:
           - Zaeder\MultiDbBundle\Security\Authentication\LoginFormAuthenticator
       logout:
         path: logout
   role_hierarchy:
     ROLE_ADMIN: [ROLE_ALLOWED_TO_SWITCH] #ROLE ADMIN is the the users without client database

登录表单

登录表单需要 serverKey、username、password 和 _csrf_token 字段

捐赠

点击这里

或使用二维码

Paypal donation