thomasvargiu / laminas-manager-registry
Doctrine Manager Registry for Laminas
0.1.0
2020-07-03 13:18 UTC
Requires
- php: ^7.2
- doctrine/orm: ^2.6
- doctrine/persistence: ^1.3 || ^2.0
- laminas/laminas-servicemanager: ^3.1
- psr/container: ^1.0
- symfony/polyfill-php73: ^1.17
Requires (Dev)
- facile-it/facile-coding-standard: ^0.3.1
- ocramius/proxy-manager: ^2.2.0
- phpspec/prophecy-phpunit: ^1.1.0 || ^2.0
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^0.12.32
- phpstan/phpstan-doctrine: ^0.12.17
- phpunit/phpunit: ^8.5.8 || ^9.2
Conflicts
This package is auto-updated.
Last update: 2024-11-06 21:36:13 UTC
README
Install
$ composer require thomasvargiu/laminas-manager-registry
Configuration
use TMV\Laminas\ManagerRegistry\EntityRepositoryFactory; use TMV\Laminas\ManagerRegistry\ManagerRegistryConfigFactory; return [ ManagerRegistryConfigFactory::class => [ 'connections' => [ 'connection1' => 'doctrine.connection.orm_default', 'connection2' => 'doctrine.connection.orm_another', ], 'managers' => [ 'manager1' => 'doctrine.entitymanager.orm_default', 'manager2' => 'doctrine.entitymanager.orm_another', ], 'default_connection' => 'connection1', // optional, default to first connection 'default_manager' => 'manager1', // optional, default to first manager ], 'dependencies' => [ 'factories' => [ // Create an EntityRepository service with a ManagerRegistry (see below) MyEntityRepository::class => new EntityRepositoryFactory(MyEntity::class), ], ], ];
Example on how to use it:
use Doctrine\Persistence\ManagerRegistry; use Doctrine\ORM\EntityManagerInterface; /** @var ManagerRegistry $managerRegistry */ $managerRegistry = $container->get(ManagerRegistry::class); /** @var EntityManagerInterface $entityManager */ $entityManager = $managerRegistry->getManager(/* <optional manager name> */); if (! $entityManager->isOpen()) { /** @var EntityManagerInterface $entityManager */ $entityManager = $managerRegistry->resetManager(/* <optional manager name> */); }
Repository
If you want to inject repositories in your services, you can extend the TMV\Laminas\ManagerRegistry\EntityRepository
:
use TMV\Laminas\ManagerRegistry\EntityRepository; class MyRepository extends EntityRepository { public function findByMyCriteria(string $value) { return $this->findBy(['my_criteria' => $value]) ; } public function anotherMethod(): void { // get the doctrine EntityRepository, using the ManagerRegistry $doctrineRepository = $this->getRepository(); // get the EntityManager, using the ManagerRegistry $entityManager = $this->getEntityManager(); } }
To instantiate your repository you can register a service using the serializable
TMV\Laminas\ManagerRegistry\EntityRepositoryFactory
factory:
use TMV\Laminas\ManagerRegistry\EntityRepositoryFactory; use TMV\Laminas\ManagerRegistry\ManagerRegistryConfigFactory; return [ 'dependencies' => [ 'factories' => [ MyRepository::class => new EntityRepositoryFactory(MyEntity::class /*, <optional manager name> */), ], ], ];