Cannot use autwire service: argument reference class, but no such service

I am upgrading a project from Symfony 3 to Symfony 4  ( https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md ) and I have many repositories / services such as:

namespace App\Entity;

use App\Entity\Activation;
use Doctrine\ORM\EntityRepository;
use Predis\Client;

class ActivationRepository extends EntityRepository
{
    // ...
}

And when I try to run the project in the browser as follows:

http://localhost:8000/login

I get this error:

(1/1) RuntimeException
Cannot autowire service "App\Entity\ActivationRepository": 
argument "$class" of method 
"Doctrine\ORM\EntityRepository::__construct()" 
references class "Doctrine\ORM\Mapping\ClassMetadata" 
but no such service exists.

Does this mean that you need to create a service for "Doctrine \ ORM \ Mapping \ ClassMetadata" in the services.yaml file?

Thanks to auto-preparation, my new services.yaml file is quite small compared to the old one, which had 2000+ lines. The new services.yaml just have a few of them (for now):

App\:
    resource: '../src/*'

# Controllers
App\Controller\:
    resource: '../src/Controller'
    autowire: true
    public: true
    tags: ['controller.service_arguments']

# Models
App\Model\:
    resource: '../src/Model/'
    autowire: true
    public: true

// etc

: services.yaml ? , , , ? , Symfony 3 Symfony 4, .

PHP 7.2.0-2 + ​​ubuntu16.04.1 + deb.sury.org + 2 (cli) (: 7 2017 20:14:31) (NTS) Linux Mint 18, Apache2 Ubuntu.

EDIT/FYI:

"Doctrine\ORM\EntityRepository:: __ construct()", ActivationRepository:

/**
     * Initializes a new <tt>EntityRepository</tt>.
     *
     * @param EntityManager         $em    The EntityManager to use.
     * @param Mapping\ClassMetadata $class The class descriptor.
     */
    public function __construct(EntityManagerInterface $em, Mapping\ClassMetadata $class)
    {
        $this->_entityName = $class->name;
        $this->_em         = $em;
        $this->_class      = $class;
    }

:

/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php
+4
2

1.8 DoctrineBundle, , Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository Doctrine\ORM\EntityRepository. , .

:

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);
    }

    // ...
}
+8

services.yaml ?

, . : EntityRepository. -. , , createQuery flush. , , , . EntityRepository, .

EntityManager :

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 interface.
}

.

0

Source: https://habr.com/ru/post/1691508/


All Articles