'"Cannot autowire service" exception when trying to clear Symfony 4 application cache
I have a project under Symfony 4 and when i try to do an composer update to my project i got this error
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
and the ActeRepository is the follow
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()
;
}
*/
}
I don't now where is the problem and the project can not update i also try to delete the var and vendor file and clear the cache of composer and Symfony but the same problem appear i don't now why i found multiple proposed solution but they not work ??
Solution 1:[1]
Constructor should look like this:
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Acte::class);
}
Add use statement:
use Doctrine\Common\Persistence\ManagerRegistry;
Extend class with:
extends ServiceEntityRepository
Solution 2:[2]
To fix this issue just remove your constructor in ActeRepository.php, it's not needed.
It's an autowire issue, so you have to init the field in your repository if you want to keep it here.
If it doesn't work, try to change the param type in your constructor :
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);
}
...
}
Solution 3:[3]
i just replace in all Repository file public function __construct(ManagerRegistry $registry) => public function __construct(\Doctrine\Common\Persistence\ManagerRegistry $registry) thinks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | igor |
| Solution 2 | |
| Solution 3 | bouakkadia brahim |
