Cannot autowire service: Argument references class but no such service exists Cannot autowire service: Argument references class but no such service exists symfony symfony

Cannot autowire service: Argument references class but no such service exists


Starting from the 1.8 version of DoctrineBundle, you can extend your class using Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository instead of Doctrine\ORM\EntityRepository. The result will be the same, but this does support the autowire.

Example:

use App\Entity\Activation;use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;use Doctrine\Common\Persistence\ManagerRegistry;class ActivationRepository extends ServiceEntityRepository{    public function __construct(ManagerRegistry $registry)    {        parent::__construct($registry, Activation::class);    }    // ...}


Do you really need to add service definitions to services.yaml for third party vendor classes?

No, don't do that. My personal suggestion is: don't extend EntityRepository. Ever. You don't want your repository's interface to have method like createQuery or flush. At least, you don't want that if you consider a repository just like a collection of objects. If you extend EntityRepository you will have a leaky abstraction.

Instead you can inject the EntityManager inside your repository, and that's it:

use App\Entity\Activation;use App\Repository\ActivationRepository;use Doctrine\ORM\EntityManagerInterface;final class DoctrineActivationRepository implements ActivationRepository{    private $entityManager;    private $repository;    public function __construct(EntityManagerInterface $entityManager)    {        $this->entityManager = $entityManager;        $this->repository = $this->entityManager->getRepository(Activation::class);    }    public function store(Activation $activation): void    {        $this->entityManager->persist($activation);        $this->entityManager->flush();    }    public function get($id): ?Activation    {        return $this->repository->find($id);    }    // other methods, that you defined in your repository's interface.}

No other steps are required.


My issue was a wrong namespace.File real position was App\Infrastructure\MySQL\Rubric\SpecificationBut namespace was set to App\Infrastructure\Rubric\Specification

Result "[blah blah] but no such service exists".