sulu / sulu

Core framework that implements the functionality of the Sulu content management system
https://sulu.io
MIT License
1.17k stars 337 forks source link

PersistenceBundle: Support Repositories which are based on ServiceEntityRepository #4364

Open trickreich opened 5 years ago

trickreich commented 5 years ago
Q A
Bug? maybe?
New Feature? yes?
Sulu Version --
Browser Version --

Actual Behavior

Currently the persistence bundle works not with such repositories. But when you use autowire to register your repositories you need something similiar.

Expected Behavior

I think it should be possible to use.

Example Repository to autowire

// src/Repository/ProductRepository.php
namespace App\Repository;

use App\Entity\Product;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

class ProductRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Product::class);
    }
} 

Possible Solutions

Something like this could work:

diff --git a/src/Sulu/Bundle/PersistenceBundle/DependencyInjection/PersistenceExtensionTrait.php b/src/Sulu/Bundle/PersistenceBundle/DependencyInje
ction/PersistenceExtensionTrait.php
index 96ba21812..157d2fc9b 100644
--- a/src/Sulu/Bundle/PersistenceBundle/DependencyInjection/PersistenceExtensionTrait.php
+++ b/src/Sulu/Bundle/PersistenceBundle/DependencyInjection/PersistenceExtensionTrait.php
@@ -11,6 +11,7 @@

 namespace Sulu\Bundle\PersistenceBundle\DependencyInjection;

+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Definition;
 use Symfony\Component\DependencyInjection\Reference;
@@ -87,11 +88,18 @@ trait PersistenceExtensionTrait
             $repositoryClass = $services['repository'];
         }

-        $definition = new Definition($repositoryClass);
-        $definition->setArguments([
-            new Reference($this->getEntityManagerServiceKey()),
-            $this->getClassMetadataDefinition($services['model']),
-        ]);
+        if (is_subclass_of($repositoryClass, ServiceEntityRepositoryInterface::class)) {
+            $definition = new Definition($repositoryClass);
+            $definition->setArguments([
+                new Reference($this->getDoctrine()),
+            ]);
+        } else {
+            $definition = new Definition($repositoryClass);
+            $definition->setArguments([
+                new Reference($this->getEntityManagerServiceKey()),
+                $this->getClassMetadataDefinition($services['model']),
+            ]);
+        }

         return $definition;
trickreich commented 5 years ago

@danrot What do you think about this?

danrot commented 5 years ago

Looks good to me. And I don't see another way in solving this anyway :slightly_smiling_face:

alexander-schranz commented 5 years ago

Looks also good for me