定制 techmobi/multidb 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

techmobi/multidb

Composer 安装命令:

composer require techmobi/multidb

包简介

Plugin for OctoberCMS allowing to assign different database to different domains

README 文档

README

OctoberCMS Plugin to create SaaS applications in different databases, making it easier to manage multiple clients. To develop this plugin I reused several classes that OctoberCMS 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.

REQUERIMENTS

Multisite

Usage

Installation

You can install this plugin either via composer.

With Composer

Execute below at the root of your project.

composer require techmobi/multidb

Configuring file database.php

You will need to make a small change to the file in config/database.php

...

'original' => env('DB_CONNECTION', 'mysql'), // <-- ADD THIS LINE

'connections' => [
	...

    //ADD THIS ARRAY
	'multidb' => [
        'driver' => 'mysql',
        'engine' => 'InnoDB',
        'host' => env('MULTIDB_HOST', 'localhost'),
        'port' => env('MULTIDB_PORT', 3306),
        'database' => env('MULTIDB_DATABASE', 'database'),
        'username' => env('MULTIDB_USERNAME', ''),
        'password' => env('MULTIDB_PASSWORD', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'varcharmax' => 191,
    ],

	...
]
...

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.

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

Added new Hosts MultiDB

Navigate to Settings -> MultiDB -> Hosts, in this place you can add new databases and list which domains will be used. At this point comes the multisite relationship. With this configuration, it will be possible to identify the databases according to each domain accessed.

Using in PHP Code

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

Trait UsesMultiConnection

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 \Techmobi\Multidb\Traits\UsesMultiConnection;

	...
}

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 UsesMultiConnection 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 \Techmobi\Multidb\Traits\UsesMainConnection;

    ...
}

BONUS: MultiFiles with MultiDB

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

class Product extends Model
{
	use \Techmobi\Multidb\Traits\UsesMultiConnection;

	...

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

    ...
}
MediaLibrary

For now I was unable to make a class to manage MediaLibrary, however you can change the file System\Classes\MediaLibrary, adding the following codes:

class MediaLibrary
{
    use \October\Rain\Support\Traits\Singleton;
    use \Techmobi\Multidb\Traits\UsesMultiConnection;

    ...

    protected function init()
    {
        $this->getDomainData();

        if (!empty($this->getDatabaseName())) {
            $this->storageFolder = "/{$this->getDatabaseName()}/media";
            $this->storagePath = "/storage/app/{$this->getDatabaseName()}/media";
        } else {
            $this->storageFolder = self::validatePath(Config::get('cms.storage.media.folder', 'media'), true);
            $this->storagePath = rtrim(Config::get('cms.storage.media.path', '/storage/app/media'), '/');
        }

        $this->ignoreNames = Config::get('cms.storage.media.ignore', FileDefinitions::get('ignoreFiles'));

        $this->ignorePatterns = Config::get('cms.storage.media.ignorePatterns', ['^\..*']);

        $this->storageFolderNameLength = strlen($this->storageFolder);
    }
    ...
}

Events Change Schema

An event has been added to force a database schema change, how does it work? Imagine that you have several databases created, however you do not want to be connecting at each URL, with that a database session was implemented, and below there is an event that you can pass a schema.

    Event::fire('techmobi.schema.set', 'my_awesome_schema');

techmobi/multidb 适用场景与选型建议

techmobi/multidb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 290 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-30