Declaring Doctrine repositories as services

Published on 2019-08-27 • Modified on 2020-02-17

[Edit 2020-02-19] Please read my blog post on this topic.
This is was my preferred way to declare and use Doctrine repositories. It allows to directly inject it wherever you need. Moreover, you have auto-completion in the IDE (PhpStorm with the Symfony plugin for example) as the service class is known. It's not the case when directly using the Doctrine getRepository() function inside a controller.


<?php

declare(strict_types=1);

namespace App\Controller\Snippet;

use App\Repository\ArticleRepository;

/**
 * I am using a PHP trait to isolate each snippet in a file.
 * This code should be called from a Symfony controller extending AbstractController (as of Symfony 4.2)
 * or Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Services are injected in the main controller constructor.
 *
 * @property ArticleRepository $articleRepo
 */
trait Snippet40Trait
{
    public function snippet40(): void
    {
        // In fact the article repository service is injected in the main controller constructor
        $articles = $this->articleRepo->findArticlesForLang('en');

        //  We've got autocompletion on the ArticleRepository functions like "findArticlesForLang".
        echo sprintf('%d posts found! 📚', \count($articles));

        // That's it! 😁
    }
}

 Run this snippet  More on Stackoverflow  More on the web  Random snippet

  Work with me!