Symfony - inject doctrine repository in service Symfony - inject doctrine repository in service symfony symfony

Symfony - inject doctrine repository in service


As you are using Symfony 3.4, you can use a much simpler approach, using ServiceEntityRepository. Simply implement your repository, let it extend class ServiceEntityRepository and you can simply inject it. (At least when using autowiring – I haven’t used this with classic DI configuration, but would assume it should also work.)

In other words:

namespace App\Repository;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Doctrine\Common\Persistence\ManagerRegistry;class ExampleRepository extends ServiceEntityRepository{    /**     * @param ManagerRegistry $managerRegistry     */    public function __construct(ManagerRegistry $managerRegistry)    {        parent::__construct($managerRegistry, YourEntity::class);    }}

Now, without any DI configuration, you can inject the repository wherever you want, including controller methods.

One caveat (which equally applies to the way you try to inject the repository): if the Doctrine connection is reset, you will have a reference to a stale repository. But IMHO, this is a risk I accept, as otherwise I won’t be able to inject the repository directly..


Check the arguments is a valid class (with FQCN or with a bundle simplification) as example:

acme.custom_repository:    class: Doctrine\ORM\EntityRepository    factory:         - '@doctrine.orm.entity_manager'        - getRepository    arguments:        - Acme\MainBundle\Entity\MyEntity

or

acme.custom_repository:    class: Doctrine\ORM\EntityRepository    factory:         - '@doctrine.orm.entity_manager'        - getRepository    arguments:        - AcmeMainBundle:MyEntity

Hope this help


Create the custom repository properly

First, you need to create the repository custom class that extends the default repository from doctrine:

use Doctrine\ORM\EntityRepository;class UserRepository extends EntityRepository{   // your own methods}

Then you need this annotation in the entity class:

/** * @ORM\Entity(repositoryClass="MyDomain\Model\UserRepository") */

Then you define the repository in the .yml file:

custom_repository:        class: MyDomain\Model\UserRepository        factory: ["@doctrine", getRepository]        arguments:          - Acme\FileBundle\Model\File

Make sure that in the definition of your repository class points to your custom repository class and not to Doctrine\ORM\EntityRepository.

Inject custom services into your custom repository:

On your custom repository create custom setters for your services

use Doctrine\ORM\EntityRepository;

class UserRepository extends EntityRepository{    protected $paginator;    public function setPaginator(PaginatorInterface $paginator)    {        $this->paginator = $paginator;    }}

Then inject them like this:

custom_repository:        class: MyDomain\Model\UserRepository        factory: ["@doctrine", getRepository]        arguments:          - Acme\FileBundle\Model\File        calls:          - [setPaginator, ['@knp_paginator']]

Inject your repository into a service:

my_custom_service:    class: Acme\FileBundle\Services\CustomService    arguments:        - "@custom_repository"