代码之家  ›  专栏  ›  技术社区  ›  Lukaszy

为什么我可以autowire EntityManagerInterface,但不能autowire UserInterface

  •  1
  • Lukaszy  · 技术社区  · 8 年前

    我编写了一个简单的服务,它使用EntityManagerInterface,它可以正常工作,但当我尝试以类似的方式添加用户界面时,我得到了:

    自动布线故障异常 无法自动连线服务“AppBundle\service\Pricer”:方法“\u 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;
        }
    }
    

    当我只有EntityManagerInterface作为参数(我可以获取存储库并进行一些查找查询)时,它就起作用了。我的错在哪里?

    1 回复  |  直到 8 年前
        1
  •  5
  •   DonCallisto    8 年前

    基本上是因为条令ORM为 EntityManagerInterface (即 EntityManager ,您可以查看 here )而Symfony没有 UserInterface .这背后的原因是 用户界面 是描述模型实体的契约/公共api的东西,而不是服务,因此这不适合服务注入的概念。