我正在使用Symfony进行简单的应用。我在这里配置的服务无法使用Symfony将资源库注入服务
services:
app.service.comments_service:
class: AppBundle\Service\CommentsService
autowire: true
app.service.projects_service:
class: AppBundle\Service\ProjectService
autowire: true
app.service.files_service:
class: AppBundle\Service\FilesService
autowire: true
app.service.users_service:
class: AppBundle\Service\UserService
autowire: true
我的服务使用存储库(注释服务使用例如注释知识库),并在这里是CommentsService
属性构造
private $entityManager;
private $session;
private $manager;
private $commentsRepository;
构造:
public function __construct(
EntityManagerInterface $entityManager,
Session $session,
ManagerRegistry $manager,CommentsRepository $commentsRepository)
{
$this->entityManager = $entityManager;
$this->session = $session;
$this->manager = $manager;
$this->commentsRepository = $commentsRepository;
}
当我尝试运行我的应用程序n我得到这个错误
PHP Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\AutowiringFailedException: Cannot autowire service "AppBundle\Repository\CommentsRepository": argument "$em" of method "Doctr ine\ORM\EntityRepository::__construct()" must have a type-hint or be given a value explicitly. Cannot autowire service "app.service.comments_service": argument "$commentsRepository" of method "AppBundle\Service\CommentsService::__construct()" references class "AppBundle\Repository\CommentsRepos itory" but no such service exists. in C:\xampp\htdocs\WINbetTaskManager\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\AutowirePass.php:285
任何想法我可以解决这个问题?
Autowire有许多限制,这就是其中之一。你需要使用一个工厂来创建一个仓库(基本上EntityManager :: getRepository(Comment :: class))你可以搜索细节并且单独定义仓库服务,我认为autowire应该选择它。 – Cerad
@Cerad我相信它应该是一个答案,而不是评论。upvoted虽然:) – svgrafov
@svgrafov谢谢,但我知道如何知识库服务工作我没有做很多与自动装配本身,所以我不知道是否会出现其他问题。换句话说,这不仅仅是一个答案,而是一个猜测。 – Cerad