final-gene/doctrine-module
Composer 安装命令:
composer require final-gene/doctrine-module
包简介
Doctrine module
README 文档
README
This module provides some help full extension for the ZF2 doctrine module.
Installation
After composer is available in your path you are ready to install this module.
$ composer require final-gene/doctrine-module
Then add FinalGene\DoctrineModule to your application config file.
Content
Entity Manager
Background
In ZF2, we retrieve our entity manager like this (example is from a factory):
<?php // ... public function createService(ServiceLocatorInterface $serviceManager) { // like this $entityManager = $serviceManager->get('Doctrine\\ORM\\EntityManager'); // or that $entityManager = $serviceManager->get('doctrine.entitymanager.orm_default'); }
The downside of this is, that you have to know which is the name of your configured manager/connection. If you deal with more than one connection or be part of a multi module project, you are not able to know which connection or entity manager belongs to your entities.
Usage
With the use of this module, the above code transforms into the following:
<?php // ... public function createService(ServiceLocatorInterface $serviceManager) { $someEntityManager = $serviceManager->get('EntityManager')->get('SomeEntity'); }
Now you get a entity manager depending on your loaded modules and their configuration.
If no one configures the DoctrineModule (or the manager within), an abstract factory is called which just load the default entity manager of doctrine.
Configuration
Simply configure your doctrine connections a described in DoctrineORMModule Documentation.
Repository Manager
Background
In ZF2, we retrieve our repositories like this (example is from a factory):
<?php public function createService(ServiceLocatorInterface $serviceManager) { $entityManager = $serviceManager->get('Doctrine\\ORM\\EntityManager'); $someRepository = $entityManager->getRepository('SomeEntity'); }
This has multiple downsides:
- changing the repositories at runtime is hard because you can't configure the behaviour of the entity manager / entity
manager's
getRepositorymethod, so you have to invent a "proxy" which then has additional logic when to switch between your different repositories - hard to test: to use an alternative repository you have to mock the getRepository function of the mighty entity manager
- hidden dependency: we don't request a dependency from the service manager but from the entity manager
- no easy using of a factory class to create your repository
Usage
With the use of this module, the above code transforms into the following:
<?php // ... public function createService(ServiceLocatorInterface $serviceManager) { $someRepository = $serviceManager->get('RepositoryManager')->get('SomeEntity'); }
Now you get a repository depending on your loaded modules and their repository manager configuration.
If no one configures the RepositoryManagerModule, an abstract factory is called which just proxies the request to
doctrines entity manager getRepository function.
Configuration
Configuration Key
The configuration key is repositories. Sub-Keys are the same as in every service manager (invokables, factories, ...).
Examples
In your module class via getConfig:
<?php // ... public function getConfig() { return [ 'repositories' => [ 'factories' => [ // your repository factories goes here // format: Entity-Name => Repository-Factory. ], ], ]; }
or via FinalGene\RepositoryManagerModule\ModuleManager\Feature\RepositoryProviderInterface (method
getRepositoryConfig)
<?php // ... public function getRepositoryConfig() { return [ 'factories' => [ // your repository factories goes here ], ]; }
or via config/autoload/repository-manager.global.config.php
return [ 'repositories' => [ 'factories' => [ // your repository factories goes here // format: Entity-Name => Repository-Factory. ], ], ];
Validators
ObjectExists
This validator will determinate wetter an object for the given parameter exists or not.
Configuration
The validator can be used and configured as any other validator. You can set the following options:
entity_class: The FQCN for the entity objectfind_method: The method in the repository to find the object
Examples
In your validators configuration:
[
// ...
[
'name' => 'FinalGene\\DoctrineModule\\Validator\\ObjectExists',
'options' => [
'entity_class' => 'Your\\Doctrine\\Entity',
'find_method' => 'find',
'messageTemplates' => [
'noObjectFound' => 'Entity with ID \'%value%\' not found',
],
],
],
];
final-gene/doctrine-module 适用场景与选型建议
final-gene/doctrine-module 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.18k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 12 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 final-gene/doctrine-module 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 final-gene/doctrine-module 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2018-12-10