dittto/doctrine-entity-factories
Composer 安装命令:
composer require dittto/doctrine-entity-factories
包简介
Allows doctrine to pull entities from factories instead of just reflection
README 文档
README
With it's usage of simple PHP objects for entities, Doctrine is a very easy-to-use ORM. It instantiates these entities using reflection. If you want to use entities that rely on another object via dependency injection, however, then you're stuck with setter injection.
This code allows you to pull new entities from a factory instead, allowing constructor injection and cleaner code.
How to use
First up, install this plugin.
composer require dittto/doctrine-entity-factories
Next, let's create a factory for our entity. These needs to implement EntityFactoryInterface.
<?php namespace App\Entities\Factories; use App\Entities\TestEntity; use Dittto\DoctrineEntityFactories\Doctrine\ORM\Mapping\EntityFactoryInterface; use Illuminate\Contracts\Validation\Validator; class TestEntityFactory implements EntityFactoryInterface { private $validator; public function __construct(Validator $validator) { $this->validator = $validator; } public function getEntity() { return new TestEntity($this->validator); } }
The more-common framework to use Doctrine with is Symfony, but you can also use Doctrine with Laravel. The following are instructions for how to use it with both.
With Laravel
To use Doctrine with Laravel, you can use this helpful plugin:
composer require laravel-doctrine/orm:1.3.* php artisan vendor:publish --tag="config"
We're going to use a custom provider and one that exists within this plugin. The custom one is as follows:
<?php namespace App\Providers; use App\Entities\Factories\TestEntityFactoryInterface; use App\Entities\TestEntity; use Dittto\DoctrineEntityFactories\Doctrine\ORM\Mapping\EntityFactoryAware; use Dittto\DoctrineEntityFactories\Doctrine\ORM\Provider\AbstractEntityFactoryServiceProvider; class EntityFactoryServiceProvider extends AbstractEntityFactoryServiceProvider { public function registerEntityFactories(EntityFactoryAware $entityFactoryRegister) { $entityFactoryRegister->addEntityFactory( TestEntity::class, new TestEntityFactory($this->app->make('hash')) ); } }
This provider defers all objects. You can also convert that new TestEntityFactory into another object easily by extending register() and provides().
Next, we'll add the providers to the main app config:
<?php // config/app.php return [ 'providers' => [ App\Providers\DoctrineServiceProvider::class, \Dittto\DoctrineEntityFactories\Doctrine\ORM\Provider\DoctrineServiceProvider::class, ], ];
Lastly, we'll need to alter the doctrine config to use our plugin:
<?php return [ 'managers' => [ 'default' => [ 'meta' => env('DOCTRINE_METADATA', 'yaml_with_entity_factories'), ] ] ];
With Symfony
As with most things with Symfony, we can use config for pretty much most things.
To start with, we're going to want to tell Symfony to use our metadata factory instead of the default Doctrine one:
# app/config/config.yml doctrine: orm: entity_managers: default: class_metadata_factory_name: Dittto\DoctrineEntityFactories\Doctrine\ORM\Mapping\ClassMetadataFactoryWithEntityFactories
If you're using multiple entity managers, this may change a bit, but you'll probably also have the understanding to make this work for your code.
Laravel, above, allows us to easily load additional code via it's service providers. For Symfony, we're going to take a slightly different approach and decorate Doctrine's EntityManager. The aim of both of these approaches is the same - to add our entity factories to it before Doctrine starts creating entities.
For Symfony, we're going to add some new services:
services: dittto.doctrine_entity_factories.entity_factory_manager_decorator: public: false class: Dittto\DoctrineEntityFactories\Doctrine\ORM\Decorator\EntityFactoryManagerDecorator decorates: doctrine.orm.default_entity_manager arguments: [ "@dittto.doctrine_entity_factories.entity_factory_manager_decorator.inner" ] calls: - [addEntityFactory, ['App\Entities\TestEntity', '@app.entities.factory.test']] app.entities.factory.test: class: App\Entities\Factories\Testfactory arguments: ['@validator']
It's here we'll use the calls definition to add in as many entity factories as we require. The first field is the full class name of the entity to be created by the factory.
The namespaces here look non-standard for Symfony, but that's purely to tie into the example above.
Testing
This plugin comes with it's own tests. To run these, clone the code and navigate to the directory. Then run the following:
composer install ./vendor/bin/phpunit
dittto/doctrine-entity-factories 适用场景与选型建议
dittto/doctrine-entity-factories 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.3k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「symfony」 「doctrine」 「factory」 「laravel」 「entity」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dittto/doctrine-entity-factories 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dittto/doctrine-entity-factories 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dittto/doctrine-entity-factories 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
The bundle for easy using json-rpc api on your project
Make pimcore migration simple
Bundle Symfony DaplosBundle
PeskyORM - annoying ORM that contains tons of exceptions and throws them when anything goes wrong
统计信息
- 总下载量: 6.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-21