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