首页 文章

无法使用Symfony将存储库注入服务

提问于
浏览
0

我正在使用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;
}

当我尝试运行我的应用程序时,我收到此错误

PHP致命错误:未捕获的Symfony \ Component \ DependencyInjection \ Exception \ AutowiringFailedException:无法自动装配服务“AppBundle \ Repository \ CommentsRepository”:方法“Doctr ine \ ORM \ EntityRepository :: __ construct()”的参数“$ em”必须具有type-hint或明确赋值 . 无法自动装配服务“app.service.comments_service”:方法“AppBundle \ Service \ CommentsService :: __ construct()”的参数“$ commentsRepository”引用类“AppBundle \ Repository \ CommentsRepos itory”但不存在此类服务 . 在C:\ xampp \ htdocs \ WINbetTaskManager \ vendor \ symfony \ symfony \ src \ Symfony \ Component \ DependencyInjection \ Compiler \ AutowirePass.php:285

我有什么想法可以解决这个问题?

1 回答

  • 1

    所以我做了一些实验,这似乎工作:

    // services.yml
    AppBundle\Repository\CommentsRepository:
        factory: 'doctrine.orm.entity_manager:getRepository'
        arguments: ['AppBundle\Entity\Comments']
    

    这应该给autowire足够的信息来注入存储库 .

相关问题