Skip to content

Commit

Permalink
bug #510 Fix declaring repository on resource when it is a service en…
Browse files Browse the repository at this point in the history
…tity repository (loic425)

This PR was merged into the 1.10 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no -->
| Related tickets |
| License         | MIT

It fixes this issue https://github.com/Sylius/SyliusResourceBundle/actions/runs/3575281362/jobs/6011624266#step:11:27

Commits
-------

aa1fdd6 Reproducing the current issue
b0ddbd2 Fix declaring a repository when it is a service entity repository
  • Loading branch information
lchrusciel authored Dec 8, 2022
2 parents 77f7a68 + b0ddbd2 commit f85f132
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Doctrine;

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ObjectManager as DeprecatedObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand Down Expand Up @@ -56,7 +57,6 @@ protected function addRepository(ContainerBuilder $container, MetadataInterface
$managerReference = new Reference($metadata->getServiceId('manager'));
$definition = new Definition($repositoryClass);
$definition->setPublic(true);

if ($repositoryClass === EntityRepository::class) {
/** @var string $entityClass */
$entityClass = $metadata->getClass('model');
Expand All @@ -68,7 +68,12 @@ protected function addRepository(ContainerBuilder $container, MetadataInterface

$genericEntities[] = $entityClass;
} else {
$definition->setArguments([$managerReference, $this->getClassMetadataDefinition($metadata)]);
if (is_a($repositoryClass, ServiceEntityRepository::class, true)) {
$definition->setArguments([new Reference('doctrine')]);
$container->setDefinition($serviceId, $definition);
} else {
$definition->setArguments([$managerReference, $this->getClassMetadataDefinition($metadata)]);
}

$container->setDefinition($serviceId, $definition);

Expand Down
13 changes: 11 additions & 2 deletions src/Bundle/test/src/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@

namespace App\Repository;

use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use App\Entity\Book;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\ResourceRepositoryTrait;

final class BookRepository extends EntityRepository implements BookRepositoryInterface
final class BookRepository extends ServiceEntityRepository implements BookRepositoryInterface
{
use ResourceRepositoryTrait;

public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Book::class);
}
}

0 comments on commit f85f132

Please sign in to comment.