sommer/wn-multidb-plugin 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sommer/wn-multidb-plugin

Composer 安装命令:

composer require sommer/wn-multidb-plugin

包简介

Plugin for WinterCMS to manage multiple databases

README 文档

README

WinterCMS Plugin to create SaaS applications in different databases, making it easier to manage multiple clients. To develop this plugin I reused several classes that WinterCMS itself has, trying to create the simplest code possible.

Important This plugin is still in the testing and development stages, use at your own risk.

Usage

Installation

You can install this plugin either via composer.

With Composer

Execute below at the root of your project.

composer require sommer/wn-multidb-plugin

Settings MultiDB - Backend

To use MultiDB you will need to follow the steps below.

Important It is necessary to have a user in the database who can Read, Edit and Update data, as well as create new databases.

Configuration .env

This is optional, but if you want to configure data from another connection in the tenants, you can put Multidb in the .env. If you don't put anything, it will automatically assume the access data from your main database server that is used in DB_*.

MULTIDB_CONNECTION=multidb
MULTIDB_HOST=127.0.0.1
MULTIDB_PORT=3306
MULTIDB_DATABASE=my_base
MULTIDB_USERNAME=root
MULTIDB_PASSWORD=

MultiDB Plugin

First Navigate to Settings -> MultiDB -> Settings Plugin, in this place you can configure the prefixes that the databases may have, configure what will be the names of each databases, and also, you can select which plugins will be replicated for the new created instances.

Save changes to update data

Using in PHP Code

After making the configurations mentioned above, it is necessary to edit the models of the plugins.

Trait UsesTenantConnection

This trait is responsible for managing the data that will be saved in the databases. It will be necessary to add the following trait to all models that have the replicated database, example below:

class Product extends Model
{
	use \Sommer\Multidb\Traits\UsesTenantConnection;

	...
}

Trait UsesMainConnection

This Trait allows you to relate a table from the main database to the child database, for example, you have a Customers plugin, this plugin is using the UsesTenantConnection trait, however ou have a Core plugin that has a Country Model, this plugin you are not replicating, but you need to relate the country_id attribute of the customers table to the country table, in this situation there would be an error because MultiDB change connections momentarily, so as not to have a problem, you need to add in your model Country the trait below.

class Product extends Model
{
    use \Sommer\Multidb\Traits\UsesMainConnection;

    ...
}

Behaviors

TenantSelectionBehavior

The TenantSelectionBehavior class added to the controller will allow the user to choose a Tenant to view once they try to access this controller (if they haven't selected one previously). At this point, this behavior adds a button next to the user's image to the navigation menu, which allows them to change the selected Tenant.

class Summaries extends Controller
{
    /**
     * @var array
     */
    public $implement = [
        'Backend\Behaviors\ListController',
        'Backend\Behaviors\FormController',
        'Sommer\MultiDB\Behaviors\TenantSelectionBehavior', // <-- add this line
    ];
}

Models Helpers

Files

If in case you want to save the files in the new database, you will need to use the model Sommer\Multidb\Models\File, example use:

class Product extends Model
{
	use \Sommer\Multidb\Traits\UsesTenantConnection;

	...

	public $attachMany = [
        'images' => [
            'Sommer\Multidb\Models\File',
            'softDelete' => true,
        ],
    ];

    ...
}

License

This plugin is released under the MIT License. You can freely use, modify, and distribute it in your projects, but please keep the original license intact.

Sponsors

If you find this plugin useful and would like to support its development, consider sponsoring the project. Your contributions help keep the project alive and improve its features. You can sponsor the project on GitHub Sponsors.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on the GitHub repository.

sommer/wn-multidb-plugin 适用场景与选型建议

sommer/wn-multidb-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 sommer/wn-multidb-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sommer/wn-multidb-plugin 我们能提供哪些服务?
定制开发 / 二次开发

基于 sommer/wn-multidb-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-26