symbiotic/database 问题修复 & 功能扩展

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

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

symbiotic/database

最新稳定版本:1.4.2

Composer 安装命令:

composer require symbiotic/database

包简介

Database connection configurator with the ability to define a connection by namespace.

README 文档

README

README.RU.md РУССКОЕ ОПИСАНИЕ

Database connection configuration package with the ability to select a connection depending on the namespace.

Installing

composer require symbiotic/database

Description

The package contains two main interfaces and a manager:

  • ConnectionsConfigInterface - Responsible for storing connections
  • NamespaceConnectionsConfigInterface - Responsible for storing namespace connections
  • DatabaseManager - Manager, contains all two interfaces, \ArrayAccess , \Stringable

Usage

Initializing Connections:

    $config = [
       'default' => 'my_connect_name',
        // Namespace connections
        'namespaces' => [
           '\\Modules\\Articles' => 'mysql_dev',
        ]
       'connections' => [
            'my_connect_name' => [
                'driver' => 'mysql',
                'database' => 'database',
                'username' => 'root',
                'password' => 'toor',
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
            ],
            'mysql_dev' => [
             // ....
            ],
        ]
    ];
    
  // Building from an array
  $manager = \Symbiotic\Database\DatabaseManager::fromArray($config);
  
  // Building via constructor
  $manager = new \Symbiotic\Database\DatabaseManager(
            new \Symbiotic\Database\ConnectionsConfig($config['connections'], $config['default']),
            new \Symbiotic\Database\NamespaceConnectionsConfig($config['namespaces']) // необязательно
        );

Methods ConnectionsConfigInterface и \ArrayAccess:

/**
 * @var \Symbiotic\Database\DatabaseManager $manager 
 */
// Getting all connections
$connections = $manager->getConnections();

// Default Connection
$defaultConnection = $manager->getDefaultConnectionName();

// Checking if a connection config exists
$bool = $manager->hasConnection('my_connect_name');
$bool = isset($manager['my_connect_name']);
 
// Getting connection data
$connectionData = $manager->getConnection('my_connect_name');
$connectionData = $manager['my_connect_name'];

// Retrieving connection data by namespace, if search engine by namespaces is enabled (description below)
$connectionData = $manager->getConnection(\Modules\PagesApplication\Models\Event::class);
 
// Adding a connection
$manager->addConnection(
         [
            'driver' => 'mysql',
            'database' => 'test_db',
            'username' => 'root',
            'password' => 'toor',
            //....
        ],
        'test_connection'
);
$manager['my_connect_name'] = [
//....
];

// Deleting a connection by name
$manager->removeConnection('test_connection');
unset($manager['test_connection']);

Methods NamespaceConnectionsConfigInterface:

/**
 * @var \Symbiotic\Database\DatabaseManager $manager 
 */

// Is the connection search by namespace active?
$bool = $manager->isActiveNamespaceFinder();

// Enable/disable search
$manager->activateNamespaceFinder(false);

// Adding a connection for a module
$manager->addNamespaceConnection('\\Modules\\PagesApplication', 'test_connection');

// Getting the name of the connection by class, if disabled, it will return null
$pagesConnectionName = $manager->getNamespaceConnection(\Modules\PagesApplication\Models\Event::class); // return `test_connection`

// Automatic connection search in the call stack, if disabled, returns null
$connectionData = $manager->findNamespaceConnectionName();

Behavioral Features

Additionally, there is a smart __toString() method. If namespace search is enabled isActiveNamespaceFinder(), it looks for a connection by namespace via the findNamespaceConnectionName() method or returns the default connection from the getDefaultConnectionName() method

Also pay attention to the behavior when the definition of connections by namespaces is disabled!

Examples:

// Configuration part
'default' => 'my_connect_name',
// Packet Connections
'namespaces' => [
   '\\Modules\\Articles' => 'mysql_dev',
]
/**
 * @var \Symbiotic\Database\DatabaseManager $manager 
 */
 // Installed Namespace from config 
namespace  Modules\Articles\Models {

$objectConnectionName = (string)$manager; //  mysql_dev (namespace connection)
$objectConnectionData = $manager->getConnection(__NAMESPACE__); //  mysql_dev config  (namespace connection)
$objectConnectionName = $manager->getNamespaceConnection(__NAMESPACE__); //  mysql_dev  (namespace connection)
$connectionData = $manager->findNamespaceConnectionName(); //  mysql_dev  (namespace connection)

// turn off detection by namespaces
$manager->activateNamespaceFinder(false);

$objectConnectionName = (string)$manager; //  my_connect_name (default)
$objectConnectionData = $manager->getConnection(__NAMESPACE__); //  NULL
$objectConnectionName = $manager->findNamespaceConnectionName();//  NULL
$objectConnectionName = $manager->getNamespaceConnection(__NAMESPACE__); // NULL
// Namespace connection can be requested directly from the config
$objectConnectionName = $manager->getNamespacesConfig()->getNamespaceConnection(__NAMESPACE__); // mysql_dev (namespace connection)

}
// Любой другой неймспейс
namespace  Modules\NewSpace\Models {

$objectConnectionName = (string)$manager; //  my_connect_name  (default)
$objectConnectionData = $manager->getConnection(__NAMESPACE__); //  NULL
$objectConnectionName = $manager->findNamespaceConnectionName();//  NULL
$objectConnectionName = $manager->getNamespaceConnection(__NAMESPACE__); // NULL
}

For Symbiotic Applications

Symbiotic framework applications have a provider to automatically establish a connection from the application settings relative to the base nemspace of the package.

  1. To add a database selection field, create a field named database_connection_name in the package settings fields:
// symbiotic.json
{
  "settings_fields": [
    {
      "label": "App Database",
      "name": "database_connection_name",
      "type": "settings::database"
    }
    /// Other settings fields...
  ]
}
  1. In the application section "app" add the provider \Symbiotic\Database\AppNamespaceConnectionProvider
// symbiotic.json
{
  "app": {
    "id": "my_app",
    //....
    "providers": [
      "\\Symbiotic\\Database\\AppNamespaceConnectionProvider" // Added provider
    ]
    /// More app settings...
  }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-03-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固