繁体   English   中英

尝试清除 Symfony 4 应用程序缓存时出现“无法自动装配服务”异常

[英]"Cannot autowire service" exception when trying to clear Symfony 4 application cache

我在 Symfony 4 下有一个项目,当我尝试对我的项目进行 Composer 更新时出现此错误

     Executing script cache:clear 
Script cache:clear returned with error code 1[KO]
!!
!!  In DefinitionErrorExceptionPass.php line 54:
!!  

!!    Cannot autowire service "App\Repository\ActeRepository": argument "$registry" of method "__construct()" references interface "Symfony\Br  
!!    idge\Doctrine\RegistryInterface" but no such service exists. Try changing the type-hint to "Doctrine\Persistence\ManagerRegistry" instea  
!!    d.

!!  

!!
!!
Script @auto-scripts was called via post-install-cmd
 [KO]
<?php

和 ActeRepository 是以下

namespace App\Repository;

use App\Entity\Acte;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method Acte|null find($id, $lockMode = null, $lockVersion = null)
 * @method Acte|null findOneBy(array $criteria, array $orderBy = null)
 * @method Acte[]    findAll()
 * @method Acte[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ActeRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Acte::class);
    }

    // /**
    //  * @return Acte[] Returns an array of Acte objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('a')
            ->andWhere('a.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('a.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Acte
    {
        return $this->createQueryBuilder('a')
            ->andWhere('a.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

我现在不知道问题出在哪里并且项目无法更新我也尝试删除 var 和供应商文件并清除 composer 和 Symfony 的缓存但出现同样的问题我现在不知道为什么我找到了多个建议的解决方案但是他们不工作?

构造函数应如下所示:

public function __construct(ManagerRegistry $registry)
{
    parent::__construct($registry, Acte::class);
}

添加使用语句:

use Doctrine\Common\Persistence\ManagerRegistry;

扩展类:

extends ServiceEntityRepository

要解决此问题,只需删除您在ActeRepository.php中的构造ActeRepository.php ,它不是必需的。

这是一个自动装配问题,因此如果您想将其保留在此处,则必须在存储库中初始化该字段。

如果它不起作用,请尝试更改构造函数中的参数类型:

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

class ActeRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Acte::class);
    }
    ...
}

我只是在所有存储库文件中替换 public function __construct(ManagerRegistry $registry) => public function __construct(\\Doctrine\\Common\\Persistence\\ManagerRegistry $registry) 认为

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM