簡體   English   中英

無法使用Symfony將存儲庫注入服務

[英]Could not inject repository into service with 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;
}

當我嘗試運行我的應用程序時,出現此錯誤

PHP致命錯誤:未捕獲的Symfony \\ Component \\ DependencyInjection \\ Exception \\ AutowiringFailedException:無法自動裝配服務“ AppBundle \\ Repository \\ CommentsRepository”:方法“ Doctr ine \\ ORM \\ EntityRepository :: __ construct()”的參數“ $ em”必須具有類型-提示或顯式地賦予其值。 無法自動裝配服務“ 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中

有什么想法我可以解決這個問題嗎?

所以我做了一些實驗,這似乎可行:

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

這應該為自動裝配提供足夠的信息以注入存儲庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM