【问题标题】:How to fix "Cannot autowire service: Argument references class but no such service exists" in Sylius?如何修复 Sylius 中的“无法自动装配服务:参数引用类但不存在此类服务”?
【发布时间】:2023-01-30 23:17:23
【问题描述】:

在 Sylius 1.11 中,在使用 maker 包创建一个新的 Campaign 实体后,我在尝试使用其存储库获取活动时遇到此错误:

无法自动连接服务“App\Repository\CampaignRepository”:方法“Doctrine\ORM\EntityRepository::__construct()”的参数“$class”引用类“Doctrine\ORM\Mapping\ClassMetadata”但不存在此类服务。

这似乎是触发错误的代码:

<?php

namespace App\Controller;

use App\Repository\CampaignRepository;

class CampaignController extends AbstractController {
    protected CampaignRepository $repository;

    public function __construct(CampaignRepository $repository) {
        $this->repository = $repository;
    }

    public function details(string $id)
    {
        $campaign = $this->repository->find($id);

        dd($campaign);
    }
}

App\Repository\CampaignRepository存在并定义如下,也就是the Sylius documentation recommends

<?php

namespace App\Repository;

use App\Entity\Campaign;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;

/**
 * @extends ServiceEntityRepository<Campaign>
 *
 * @method Campaign|null find($id, $lockMode = null, $lockVersion = null)
 * @method Campaign|null findOneBy(array $criteria, array $orderBy = null)
 * @method Campaign[]    findAll()
 * @method Campaign[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class CampaignRepository extends EntityRepository
{
}

如何修复此错误?

【问题讨论】:

    标签: php symfony sylius


    【解决方案1】:

    长话短说

    事实证明,Sylius 在类型提示和变量名上都非常挑剔。这是由于 Symfony 的依赖注入的工作方式以及 Sylius 选择使用它的方式。

    将构造函数更改为:

    public function __construct(
       SyliusBundleResourceBundleDoctrineORMEntityRepository $campaignRepository
    ) {
        $this->repository = $campaignRepository;
    }
    

    你怎么猜的?

    您需要使用bin/console debug:container 命令。但是如果您尝试,您将遇到与运行控制器操作时相同的错误消息。

    你需要做的是:

    1. 注释任何使用 symfony 的依赖注入特性来访问存储库实例的方法(在我们的例子中是 AppControllerCampaignController::__construct() 方法)
    2. 运行bin/console debug:container CampaignRepository
      这将为您返回服务列表:
      bin/console debug:container CampaignRepository
      
      Select one of the following services to display its information:
      [0] AppRepositoryCampaignRepository
      [1] DoctrinePersistenceObjectRepository $campaignRepository
      [2] DoctrineCommonCollectionsSelectable $campaignRepository
      [3] SyliusComponentResourceRepositoryRepositoryInterface $campaignRepository
      [4] SyliusBundleResourceBundleDoctrineORMEntityRepository $campaignRepository
      [5] DoctrineORMEntityRepository $campaignRepository
      
      1. 选择类名和参数的组合作为你的构造函数参数(我选择了SyliusComponentResourceRepositoryRepositoryInterface $campaignRepository,但任何带有参数名的建议都应该有效)
      2. 取消注释您在步骤 1 中注释的方法并更新其签名
      3. 瞧瞧

      如果你好奇它是如何工作的,请看这里:https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2013-08-15
      相关资源
      最近更新 更多