streamcommon / doctrine-manager
Doctrine 2 容器互操作
1.2.6
2022-06-06 08:24 UTC
Requires
- php: ^7.3 || ^8.0
- doctrine/dbal: ^2.9.2
- doctrine/event-manager: ^1.0.0
- doctrine/migrations: ^3.0.0
- doctrine/orm: ^2.6.3
- psr/container: ^1.0
- streamcommon/excess-configuration: ^1.0.0
Requires (Dev)
- ext-memcached: *
- ext-pdo: *
- ext-pdo_sqlite: *
- ext-redis: *
- jsoumelidis/zend-sf-di-config: ^0.4.0
- laminas/laminas-auradi-config: ^1.0.1
- laminas/laminas-pimple-config: ^1.1.0
- laminas/laminas-servicemanager: ^3.4.0
- malukenho/docheader: ^0.1.7
- php-coveralls/php-coveralls: ^2.1.0
- phpspec/prophecy-phpunit: dev-master
- phpstan/phpstan: ^0.12.18
- phpunit/phpunit: ^9.1.1
- predis/predis: ^1.1.0
- roave/security-advisories: dev-master
- streamcommon/coding-standard: dev-master
This package is auto-updated.
Last update: 2024-09-19 21:25:50 UTC
README
此包提供了 Doctrine 2 工厂,用于符合 PRS-11 容器标准。
分支
安装
控制台运行
composer require streamcommon/doctrine-manager
或者添加到你的 composer.json
"require": { "streamcommon/doctrine-manager": "*" }
请查阅 doctrine 文档以获取更多信息
- ORM 版本 2.6
- DBAL 版本 2.9
- 迁移版本 2.1
- 事件管理器版本 1.0
- 元数据驱动
- ORM 事件
- Doctrine 注释 >1.6
- 缓存
- 实体管理器
- ZendFramework/zend-servicemanager
- ZendFramework/zend-auradi-config
- ZendFramework/zend-pimple-config
- Jsoumelidis/zend-sf-di-config
- Laminas/laminas-servicemanager
- Laminas/laminas-auradi-config
- Laminas/laminas-pimple-config
示例配置项目文件
Psr\Container\ContainerInterface
容器必须具有config
键
配置你的项目配置文件
- 配置 doctrine 配置,例如
'config' => [ 'doctrine' => [ 'configuration' => [ // If you use single connection 'orm_default' => [ 'result_cache' => 'array', 'metadata_cache' => 'array', 'query_cache' => 'array', 'hydration_cache' => 'array', 'driver' => 'orm_default', ], // If you want to add a second connection 'orm_custom' => [ 'result_cache' => 'memcached', 'metadata_cache' => 'memcached', 'query_cache' => 'memcached', 'hydration_cache' => 'memcached', 'driver' => 'orm_custom', ], ],
- 配置连接选项,例如
'connection' => [ // If you use single connection // Default using MySql connection 'orm_default' => [ 'configuration' => 'orm_default', 'event_manager' => 'orm_default', 'params' => [ 'dbname' => 'name', 'user' => 'user', 'password' => 'password', 'host' => 'localhost', ], ], // If you want to add a second connection // Alternative Postgress connection 'orm_custom' => [ 'configuration' => 'orm_custom', 'event_manager' => 'orm_custom', 'driver_class_name' => \Doctrine\DBAL\Driver\PDOPgSql\Driver::class, 'params' => [ 'dbname' => 'name', 'user' => 'user', 'password' => 'password', 'host' => 'localhost_custom', ], ] ],
- 配置实体|事件管理器
'entity_manager' => [ 'orm_default' => [ 'connection' => 'orm_default', 'configuration' => 'orm_default', ], 'orm_custom' => [ 'connection' => 'orm_custom', 'configuration' => 'orm_custom', ] ], 'event_manager' => [ 'orm_default' => [ 'subscribers' => [], ], 'orm_custom' => [ 'subscribers' => [], ] ], 'entity_resolver' => [ 'orm_default' => [ 'resolvers' => [], ], 'orm_custom' => [ 'resolvers' => [], ], ],
- 配置 ORM 驱动,例如
'driver' => [ // If you use single connection // Annotation driver example //@see https://www.doctrine-project.org/projects/doctrine-annotations/en/1.6/index.html 'orm_default' => [ 'class_name' => \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain::class, 'drivers' => [ 'Annotation\Entity' => 'Annotation\Entity' ], ], 'Annotation\Entity' => [ 'class_name' => \Doctrine\ORM\Mapping\Driver\AnnotationDriver::class, 'paths' => [ __DIR__ . '/Annotation/Entity' ] ], // If you want to add a second connection // Php driver for example 'orm_custom' => [ 'class_name' => \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain::class, 'drivers' => [ 'PHPDriver\Entity' => 'PHPDriver\Entity' ], ], 'PHPDriver\Entity' => [ 'class_name' => \Doctrine\Common\Persistence\Mapping\Driver\PHPDriver::class, 'paths' => [ __DIR__ . '/PHPDriver/Entity' ] ], ],
- 配置 doctrine 缓存
//@see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/caching.html 'cache' => [ 'array' => [ 'class_name' => Doctrine\Common\Cache\ArrayCache::class, 'namespace' => 'Streamcommon\Doctrine\Manager\Interop', ] ], ] ],
- 配置你的项目依赖
use Streamcommon\Doctrine\Manager\Common\Factory\{ Cache as CacheFactory, Driver as DriverFactory, EventManager as EventManagerFactory }; use Streamcommon\Doctrine\Manager\DBAL\Factory\Connection as ConnectionFactory; use Streamcommon\Doctrine\Manager\ORM\Factory\{ Configuration as ConfigurationFactory, EntityManager as EntityManagerFactory, EntityResolver as EntityResolverFactory, }; 'dependencies' => [ 'factories' => [ // If you use single connection 'doctrine.driver.orm_default' => [DriverFactory::class, 'orm_default'], 'doctrine.event_manager.orm_default' => [EventManagerFactory::class, 'orm_default'], 'doctrine.configuration.orm_default' => [ConfigurationFactory::class, 'orm_default'], 'doctrine.connection.orm_default' => [ConnectionFactory::class, 'orm_default'], 'doctrine.entity_resolver.orm_default' => [EntityResolverFactory::class, 'orm_default'], 'doctrine.entity_manager.orm_default' => [EntityManagerFactory::class, 'orm_default'], 'doctrine.cache.array' => [CacheFactory::class, 'orm_default'], // If you want to add a second connection 'doctrine.driver.orm_custom' => [DriverFactory::class, 'orm_custom'], 'doctrine.event_manager.orm_custom' => [EventManagerFactory::class, 'orm_custom'], 'doctrine.configuration.orm_custom' => [ConfigurationFactory::class, 'orm_custom'], 'doctrine.connection.orm_custom' => [ConnectionFactory::class, 'orm_custom'], 'doctrine.entity_resolver.orm_custom' => [EntityResolverFactory::class, 'orm_custom'], 'doctrine.entity_manager.orm_custom' => [EntityManagerFactory::class, 'orm_custom'], ], ]
- 在你的项目中使用
$em = $container->get('doctrine.entity_manager.orm_default'); $connection = $container->get('doctrine.connection.orm_default');
示例配置容器
Laminas 服务管理器
use Streamcommon\Doctrine\Manager\ConfigProvider; use Laminas\ServiceManager\ServiceManager; $config = new ConfigProvider(); $config = $config(); $dependencies = $config['dependencies']; $dependencies['services']['config'] = $config; return new ServiceManager($dependencies);
Symfony 容器
use JSoumelidis\SymfonyDI\Config\{Config as SymfonyConfig, ContainerFactory as SymfonyContainerFactory}; use Streamcommon\Doctrine\Manager\ConfigProvider; $config = new ConfigProvider(); $config = $config(); $dependencies = $config['dependencies']; $dependencies['services']['config'] = $config; $container = new SymfonyContainerFactory(); return $container(new SymfonyConfig($dependencies))
等等...
CLI 使用
- 查看
- 对于多个连接,为
orm
命名空间添加了一个新的--object-manager
参数
Arguments: --object-manager Doctrine object manager name [default: "orm_default"]