From 5e3c593d88023a79b1bb90a23e6f7ffeec7ba618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Mon, 25 Feb 2019 11:55:23 +0100 Subject: [PATCH 1/5] Update OrmExtension.php --- src/DI/OrmExtension.php | 57 ++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/src/DI/OrmExtension.php b/src/DI/OrmExtension.php index 6f03169..9319405 100644 --- a/src/DI/OrmExtension.php +++ b/src/DI/OrmExtension.php @@ -10,6 +10,7 @@ use Nette\DI\Helpers; use Nette\DI\Statement; use Nette\InvalidArgumentException; +use Nette\Utils\Strings; use Nettrine\ORM\EntityManagerDecorator; use Nettrine\ORM\Exception\Logical\InvalidStateException; use Nettrine\ORM\ManagerRegistry; @@ -129,26 +130,42 @@ public function loadEntityManagerConfiguration(): void throw new InvalidStateException(sprintf('EntityManagerDecorator class "%s" not found', $entityManagerDecoratorClass)); } - // Entity Manager - $original = $builder->addDefinition($this->prefix('entityManager')) - ->setType(DoctrineEntityManager::class) - ->setFactory(DoctrineEntityManager::class . '::create', [ - $builder->getDefinitionByType(Connection::class), // Nettrine/DBAL - $this->prefix('@configuration'), - ]) - ->setAutowired(false); - - // Entity Manager Decorator - $builder->addDefinition($this->prefix('entityManagerDecorator')) - ->setFactory($entityManagerDecoratorClass, [$original]); - - // ManagerRegistry - $builder->addDefinition($this->prefix('managerRegistry')) - ->setType(ManagerRegistry::class) - ->setArguments([ - $builder->getDefinitionByType(Connection::class), - $this->prefix('@entityManagerDecorator'), - ]); + foreach ($builder->findByType(\Doctrine\DBAL\Connection::class) as $definitionName => $serviceDefinition) { + $match = Strings::match($definitionName, '~([a-zA-Z]+\.([a-zA-Z]+))\.connection~'); + + if ($serviceDefinition->getTag(\Nettrine\DBAL\DI\DbalExtension::TAG_CONNECTION) !== null && array_key_exists(1, $match) && array_key_exists(2, $match)) { + $nameWithPrefix = $match[1]; + $name = $match[2]; + + } else { + continue; + } + + // Entity Manager + $original = $builder->addDefinition($this->prefix($name . '.entityManager')) + ->setType(DoctrineEntityManager::class) + ->setFactory(DoctrineEntityManager::class . '::create', [ + $builder->getDefinition($nameWithPrefix . '.connection'), // Nettrine/DBAL + $this->prefix('@configuration'), + ]) + ->setAutowired(false); + + $autowired = $name === \Nettrine\DBAL\DI\DbalExtension::DEFAULT_CONNECTION_NAME ? true : false; + + // Entity Manager Decorator + $builder->addDefinition($this->prefix($name . '.entityManagerDecorator')) + ->setFactory($entityManagerDecoratorClass, [$original]) + ->setAutowired($autowired); + + // ManagerRegistry + $builder->addDefinition($this->prefix($name . '.managerRegistry')) + ->setType(ManagerRegistry::class) + ->setArguments([ + $builder->getDefinition($nameWithPrefix . '.connection'), + $this->prefix('@' . $name . '.entityManagerDecorator'), + ]) + ->setAutowired($autowired); + } } } From aba8e5f28f5230bf687c18dd47f458a578c33ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Fri, 5 Apr 2019 09:58:18 +0200 Subject: [PATCH 2/5] Update README.md --- .docs/README.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/.docs/README.md b/.docs/README.md index 5749f23..e6c6054 100755 --- a/.docs/README.md +++ b/.docs/README.md @@ -32,12 +32,15 @@ And set-up DBAL connection. ```yaml dbal: - connection: - host: 127.0.0.1 - user: root - password: - dbname: nettrine - #driver: pdo_pgsql + connections: + default: + host: 127.0.0.1 + user: root + password: + dbname: nettrine + #driver: pdo_pgsql + another_connection: + ... ``` Secondly, enable the Doctrine ORM extension. It's provided by this package. From 1a16d6944ab0306ebcfc25b9af1c8cab4e2abfd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Fri, 5 Apr 2019 10:01:33 +0200 Subject: [PATCH 3/5] Update OrmExtension.php --- src/DI/OrmExtension.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/DI/OrmExtension.php b/src/DI/OrmExtension.php index 9319405..f396f93 100644 --- a/src/DI/OrmExtension.php +++ b/src/DI/OrmExtension.php @@ -131,38 +131,28 @@ public function loadEntityManagerConfiguration(): void } foreach ($builder->findByType(\Doctrine\DBAL\Connection::class) as $definitionName => $serviceDefinition) { - $match = Strings::match($definitionName, '~([a-zA-Z]+\.([a-zA-Z]+))\.connection~'); - - if ($serviceDefinition->getTag(\Nettrine\DBAL\DI\DbalExtension::TAG_CONNECTION) !== null && array_key_exists(1, $match) && array_key_exists(2, $match)) { - $nameWithPrefix = $match[1]; - $name = $match[2]; - - } else { - continue; - } - // Entity Manager - $original = $builder->addDefinition($this->prefix($name . '.entityManager')) + $original = $builder->addDefinition($this->prefix($definitionName . '.entityManager')) ->setType(DoctrineEntityManager::class) ->setFactory(DoctrineEntityManager::class . '::create', [ - $builder->getDefinition($nameWithPrefix . '.connection'), // Nettrine/DBAL + $builder->getDefinition($definitionName . '.connection'), // Nettrine/DBAL $this->prefix('@configuration'), ]) ->setAutowired(false); - $autowired = $name === \Nettrine\DBAL\DI\DbalExtension::DEFAULT_CONNECTION_NAME ? true : false; + $autowired = $serviceDefinition->isAutowired(); // Entity Manager Decorator - $builder->addDefinition($this->prefix($name . '.entityManagerDecorator')) + $builder->addDefinition($this->prefix($definitionName . '.entityManagerDecorator')) ->setFactory($entityManagerDecoratorClass, [$original]) ->setAutowired($autowired); // ManagerRegistry - $builder->addDefinition($this->prefix($name . '.managerRegistry')) + $builder->addDefinition($this->prefix($definitionName . '.managerRegistry')) ->setType(ManagerRegistry::class) ->setArguments([ - $builder->getDefinition($nameWithPrefix . '.connection'), - $this->prefix('@' . $name . '.entityManagerDecorator'), + $builder->getDefinition($definitionName . '.connection'), + $this->prefix('@' . $definitionName . '.entityManagerDecorator'), ]) ->setAutowired($autowired); } From 0617f7bf36b38dfff2c9e14c75e3ca7aa7509053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Fri, 5 Apr 2019 10:12:27 +0200 Subject: [PATCH 4/5] Update OrmExtension.php --- src/DI/OrmExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DI/OrmExtension.php b/src/DI/OrmExtension.php index f396f93..7eedfc0 100644 --- a/src/DI/OrmExtension.php +++ b/src/DI/OrmExtension.php @@ -135,7 +135,7 @@ public function loadEntityManagerConfiguration(): void $original = $builder->addDefinition($this->prefix($definitionName . '.entityManager')) ->setType(DoctrineEntityManager::class) ->setFactory(DoctrineEntityManager::class . '::create', [ - $builder->getDefinition($definitionName . '.connection'), // Nettrine/DBAL + $builder->getDefinition($definitionName), // Nettrine/DBAL $this->prefix('@configuration'), ]) ->setAutowired(false); @@ -151,7 +151,7 @@ public function loadEntityManagerConfiguration(): void $builder->addDefinition($this->prefix($definitionName . '.managerRegistry')) ->setType(ManagerRegistry::class) ->setArguments([ - $builder->getDefinition($definitionName . '.connection'), + $builder->getDefinition($definitionName), $this->prefix('@' . $definitionName . '.entityManagerDecorator'), ]) ->setAutowired($autowired); From 113c38970b4096e77b2c7b4edbfba4a7dbe1691a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1?= Date: Fri, 5 Apr 2019 10:19:28 +0200 Subject: [PATCH 5/5] Update OrmExtension.php --- src/DI/OrmExtension.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DI/OrmExtension.php b/src/DI/OrmExtension.php index 7eedfc0..815c322 100644 --- a/src/DI/OrmExtension.php +++ b/src/DI/OrmExtension.php @@ -10,7 +10,6 @@ use Nette\DI\Helpers; use Nette\DI\Statement; use Nette\InvalidArgumentException; -use Nette\Utils\Strings; use Nettrine\ORM\EntityManagerDecorator; use Nettrine\ORM\Exception\Logical\InvalidStateException; use Nettrine\ORM\ManagerRegistry; @@ -130,7 +129,7 @@ public function loadEntityManagerConfiguration(): void throw new InvalidStateException(sprintf('EntityManagerDecorator class "%s" not found', $entityManagerDecoratorClass)); } - foreach ($builder->findByType(\Doctrine\DBAL\Connection::class) as $definitionName => $serviceDefinition) { + foreach ($builder->findByType(Connection::class) as $definitionName => $serviceDefinition) { // Entity Manager $original = $builder->addDefinition($this->prefix($definitionName . '.entityManager')) ->setType(DoctrineEntityManager::class)