为什么我可以自动装配EntityManagerInterface而不是UserInterface

Luk*_*szy 1 symfony symfony-3.4

我编写了使用EntityManagerInterface的简单服务并且它正在工作但是当我以类似的方式尝试添加UserInterface我得到:

AutowiringFailedException无法自动装配服务"AppBundle\Service\Pricer":方法"__construct()"的参数"$ user"引用接口"Symfony\Component\Security\Core\User\UserInterface"但不存在此类服务.它无法自动注册,因为它来自不同的根命名空间.

我的代码是:

namespace AppBundle\Service;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class Pricer
{
    private $em;
    private $user;

    public function __construct(EntityManagerInterface $em, UserInterface $user)
    {
        $this->em = $em;
        $this->user = $user;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我只有EntityManagerInterface作为参数时,它正在工作(我可以获取存储库并进行一些查找查询).我的错误在哪里?

Don*_*sto 5

基本上是因为Doctrine ORM提供了一个默认实现EntityManagerInterface(也就是说EntityManager,你可以在这里查看),而Symfony则没有UserInterface.这背后的原因是,UserInterface它描述了模型实体的合同/公共API,而不是服务,因此这不符合服务注入的概念.