arturdoruch / doctrine-entity-manager-bundle
Helps to create and access to the own doctrine entity manager classes.
Installs: 40
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- doctrine/doctrine-bundle: ^1.10|^2.0
- doctrine/orm: ^2.7
README
Symfony Bundle helping to create and access to the own Doctrine entity manager classes (with operations doing on the entity).
Installation
composer require arturdoruch/doctrine-entity-manager-bundle
Register bundle in kernel class.
$bundles = [ new ArturDoruch\DoctrineEntityManagerBundle\ArturDoruchDoctrineEntityManagerBundle(), ];
Usage
Creating entity manager
In order to creating Doctrine entity manager class extend the
ArturDoruch\DoctrineEntityManagerBundle\AbstractEntityManager
abstract class,
and implement required getRepository()
method returning the repository class of managing entity.
Example:
<?php namespace AppBundle\Doctrine; use AppBundle\Entity\Foo; use ArturDoruch\DoctrineEntityManagerBundle\AbstractEntityManager; class FooManager extends AbstractEntityManager { public function getRepository() { return $this->doGetRepository(Foo::class); } // Custom entity manager methods. /*public function create(): Foo { $foo = new Foo(); return $foo; } public function save(Foo $foo) { $this->persist($foo); } public function remove(Foo $foo) { $this->doRemove($foo); }*/ }
Register entity manager as service
To register entity manager as service, add the arturdoruch.doctrine_entity_manager
tag
to the service configuration.
doctrine.foo_manager: class: AppBundle\Doctrine\FooManager tags: ['arturdoruch.doctrine_entity_manager']
Accessing entity managers
All managers are registered in ArturDoruch\DoctrineEntityManagerBundle\EntityManagerRegistry
class.
To get specific entity manager pass it class name to the get()
method.
In controller:
<?php $emr = $this->get('arturdoruch.doctrine_entity_manager_registry'); $fooManager = $emr->get(FooManager::class); $barManager = $emr->get(BarManager::class);
In entity manager:
<?php namespace AppBundle\Doctrine; use AppBundle\Entity\Foo; use AppBundle\Doctrine\BarManager; use ArturDoruch\DoctrineEntityManagerBundle\AbstractEntityManager; class FooManager extends AbstractEntityManager { /** * @var BarManager */ private $barManager; public function initialize() { $this->barManager = $this->getManager(BarManager::class); } }
Class API
ArturDoruch\DoctrineEntityManagerBundle\AbstractEntityManager
Protected methods for use in the entity manager class:
- getEntityManagerName
- initialize
- getManager
- persist
- doRemove