-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #220 Duplicate initialisation of repositories (Justus Krapp)
This PR was merged into the 1.7-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | fixes #217 | License | MIT Accessing a repository via `EntityManger::getReposiory` and direct injection via the dot notation of the repository will yield different instances of the same repository. This is caused by: * Repositories are not injected into the ServiceLocator of [ContainerRepositoryFactory](https://github.com/doctrine/DoctrineBundle/blob/1.12.x/Repository/ContainerRepositoryFactory.php) as the required tag `doctrine.repository_service` is missing for our generated the repositories * Even when injected (after adding that `doctrine.repository_service`) the [ContainerRepositoryFactory::getRepository](https://github.com/doctrine/DoctrineBundle/blob/1.12.x/Repository/ContainerRepositoryFactory.php#L35) method cant resolve the repository as its injected with the dot notation and doctrine looks for the repository by class name * Problem also exists for default repositories but simply tagging them does not work Solution/Changes; For the solid repositories: * **Tag the solid repositories** so they get injected * **Switch to service id to class name** * **Create a public alias** to maintain old functionality For generic default repositories (cant be tagged as all use the same class) * **Init default repositories via doctrine factory** so its using `ContainerRepositoryFactory` and by that prevent duplicate initialisation * Decorated `ContainerRepositoryFactory` to be table to return the sylius `EntityRepository` instead of the default doctrine one Commits ------- 2baf357 Preventing the duplicate initialisation of repositories when access via entity manager and dependency injection 117fb44 Preventing the duplicate initialisation of repositories when access via entity manager and dependency injection 3ceb1ba removes possible bc break
- Loading branch information
Showing
4 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\Doctrine; | ||
|
||
use Doctrine\Bundle\DoctrineBundle\Repository\ContainerRepositoryFactory as DoctrineContainerRepositoryFactory; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Repository\RepositoryFactory; | ||
use Doctrine\Persistence\ObjectRepository; | ||
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; | ||
|
||
final class ContainerRepositoryFactory implements RepositoryFactory | ||
{ | ||
/** @var ObjectRepository[] */ | ||
private $managedRepositories = []; | ||
|
||
/** @var string[] */ | ||
private $genericEntities = []; | ||
|
||
/** @var DoctrineContainerRepositoryFactory */ | ||
private $doctrineFactory; | ||
|
||
public function __construct(DoctrineContainerRepositoryFactory $doctrineFactory) | ||
{ | ||
$this->doctrineFactory = $doctrineFactory; | ||
} | ||
|
||
public function addGenericEntity(string $entityName): void | ||
{ | ||
$this->genericEntities[$entityName] = $entityName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRepository(EntityManagerInterface $entityManager, $entityName): ObjectRepository | ||
{ | ||
if (isset($this->genericEntities[$entityName])) { | ||
$metadata = $entityManager->getClassMetadata($entityName); | ||
|
||
return $this->getOrCreateRepository($entityManager, $metadata); | ||
} | ||
|
||
return $this->doctrineFactory->getRepository($entityManager, $entityName); | ||
} | ||
|
||
private function getOrCreateRepository( | ||
EntityManagerInterface $entityManager, | ||
ClassMetadata $metadata | ||
): ObjectRepository { | ||
$repositoryHash = $metadata->getName() . spl_object_hash($entityManager); | ||
|
||
if (isset($this->managedRepositories[$repositoryHash])) { | ||
return $this->managedRepositories[$repositoryHash]; | ||
} | ||
|
||
return $this->managedRepositories[$repositoryHash] = new EntityRepository($entityManager, $metadata); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters