继承模型和封装连接管理器以避免与Laravel冲突
1.4.2
2023-04-12 16:15 UTC
Requires
- php: >=8.0
- illuminate/database: >=9.x-dev
- symbiotic/database: ~1.4
This package is auto-updated.
Last update: 2024-09-12 19:15:32 UTC
README
README.RU.md 俄语描述
Eloquent包装包(laravel/database
),用于并行独立工作与独立应用程序的Laravel模型。
安装
composer require symbiotic/eloquent
描述
当第三方功能使用laravel/database
包时,与连接一起工作时会发生配置冲突。当前包继承模型和管理器类,这使得您可以在Laravel中并行使用多个独立库,它们有各自的连接设置并使用Laravel模型。还可能通过基本命名空间配置连接,这使得每个包都可以指定其自己的连接。
使用方法
管理器初始化
$config = [ 'default' => 'my_connect_name', // Connections by package namespaces '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 of data {@link https://github.com/symbiotic-php/database} $connectionsConfig = \Symbiotic\Database\DatabaseManager::fromArray($config); // Manager initialization $manager = new \Symbiotic\Database\Eloquent\EloquentManager( $connectionsConfig, \Symbiotic\Database\Eloquent\SymbioticModel::class // Base model class // If you are using Laravel models - \Illuminate\Database\Eloquent\Model::class ); // Additionally, you can install the Event Manager (\Illuminate\Contracts\Events\Dispatcher) $manager->setEventDispatcher(new Dispatcher()); // Activating your connection manager for models $manager->bootEloquent(); // Your code and requests.... // Extracting the current connection manager and installing the previous one (if any) $manager->popEloquent();
初始化Laravel基本模型\Illuminate\Database\Eloquent\Model::class
时,您将全局覆盖模型所有后代的管理器连接,所以请确保在功能完成后检查您的连接管理器。您可以使用继承的模型\Symbiotic\Database\Eloquent\SymbioticModel::class
作为基础,这样可以消除所有配置冲突。
建议继承自SymbioticModel类
use Symbiotic\Database\Eloquent\SymbioticModel; class User extends SymbioticModel { //.... }
基本方法
/** * @var \Symbiotic\Database\Eloquent\EloquentManager $manager */ // Returns the Laravel connection manager {@see \Illuminate\Database\DatabaseManager} $laravelDatabase = $manager->getDatabaseManager(); // Getting a Connection Object {@see \Illuminate\Database\Connection} // if called without a parameter, it will return the connection 'default' $connection = $manager->getConnection($connectionName ?? null); // Adding a connection $manager->addConnection( [ 'driver' => 'mysql', 'database' => 'test_db', 'username' => 'root', 'password' => 'toor', //.... ], 'test_connection' ); // Installing an Event Dispatcher for Models {@uses \Illuminate\Contracts\Events\Dispatcher} $manager->setEventDispatcher($dispatcher); // Getting the event dispatcher, if the dispatcher is not set, will return the anonymous class NullDispatcher $dispatcher = $manager->getEventDispatcher(); // Force model connection switching at runtime $manager->withConnection('connection_name', function() { $model = new MyModel(); $model->save([]); });
根据模型命名空间配置连接
有时一些独立功能需要放置在单独的数据库中,为此,您可以使用基于命名空间的连接配置。
/** * @var \Symbiotic\Database\Eloquent\EloquentManager $manager * @var \Symbiotic\Database\DatabaseManager $symbioticDatabase */ // Returns the Symbiotic connection manager {@see \Symbiotic\Database\DatabaseManager} // Read the documentation of the manager and the features of his behavior {@link https://github.com/symbiotic-php/database} $symbioticDatabase = $manager->getSymbioticDatabaseManager(); // Is the connection search by namespace active? $bool = $symbioticDatabase->isActiveNamespaceFinder(); // Enable/disable search by namespaces $symbioticDatabase->activateNamespaceFinder(false); // Adding a separate connection for a module $symbioticDatabase->addNamespaceConnection('\\Modules\\PagesApplication', 'test_connection'); // Getting the name of the connection by class, if disabled, it will return null $pagesConnectionName = $symbioticDatabase->getNamespaceConnection(\Modules\PagesApplication\Models\Event::class); // return `test_connection` // Automatic search for a connection along the call stack via debug_backtrace, if disabled, returns null $connectionData = $symbioticDatabase->findNamespaceConnectionName();
多个独立管理器可以协同工作和跨功能工作
/** * @var \Symbiotic\Database\Eloquent\EloquentManager $manager */ $capsuleOne = new \Symbiotic\Database\Eloquent\EloquentManager($config,\Illuminate\Database\Eloquent\Model::class); $capsuleTwo = new \Symbiotic\Database\Eloquent\EloquentManager($config,\Symbiotic\Database\Eloquent\SymbioticModel::class); $capsuleThree = new \Symbiotic\Database\Eloquent\EloquentManager($config,\Illuminate\Database\Eloquent\Model::class); $capsuleOne->bootEloquent(); // we work with the first manager $capsuleOne->popEloquent(); $capsuleThree->bootEloquent(); // activate two managers at once $capsuleOne->bootEloquent(); $capsuleTwo->bootEloquent(); // working with a second manager $capsuleTwo->popEloquent(); // we work with the first manager $capsuleOne->popEloquent(); // working with a third manager $capsuleOne->popEloquent(); // terminated managers, if Laravel is initiated its connection manager will be installed back
获取迁移的SchemaBuilder
/** * @var \Symbiotic\Database\Eloquent\EloquentManager $manager */ $schema = $manager->getSchemaBuilder(); $schema->create('table_name', static function(\Illuminate\Database\Schema\Blueprint $table) { $blueprint->string('name', 255); }); $schema->drop('table_name');
关于使用模型和设置连接的完整文档