无法自动连接服务:参数引用类,但不存在此类服务

2022-08-30 11:52:33

我正在将一个项目从Symfony 3升级到Symfony 4https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md),我有很多这样的存储库/服务:

namespace App\Entity;

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

class ActivationRepository extends EntityRepository
{
    // ...
}

当我尝试在浏览器中运行项目时,如下所示:

http://localhost:8000/login

我收到此错误:

(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.

这是否意味着您必须在 services.yaml 文件中为“Doctrine\ORM\Mapping\ClassMetadata”创建一个服务?

多亏了自动布线,我的新服务.yaml文件与旧文件相比相当小,旧文件有2000多行。新的services.yaml只有其中的几个(到目前为止):

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) (构建: Dec 7 2017 20:14:31) ( NTS ) Linux Mint 18, Apache2 Ubuntu.

编辑/仅供参考:

这是“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

答案 1

从 1.8 版本的 DoctrineBundle 开始,您可以使用 代替 来扩展您的类。结果将是相同的,但这确实支持自动布线。Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryDoctrine\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);
    }

    // ...
}

答案 2

是否真的需要为第三方供应商类向 services.yaml 添加服务定义?

不,不要那样做。我个人的建议是:不要扩展。曾。您不希望存储库的接口具有类似 或 的方法。至少,如果您将存储库视为对象集合,则不希望这样做。如果你扩展,你将有一个泄漏的抽象。EntityRepositorycreateQueryflushEntityRepository

相反,您可以在存储库中注入,仅此而已: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's interface.
}

不需要其他步骤。


推荐