小编Luk*_*szy的帖子

带 getManager 和不带 getManager 的 getRepository 有什么区别?

我使用了代码:

    $userRepository = $this->getDoctrine()
                ->getManager()
                ->getRepository(User::class);

但今天在分析一些教程时我发现没有 getManager 的代码也有同样的作用

    $userRepository = $this->getDoctrine()
                ->getRepository(User::class);

那么,有什么区别呢?细节决定成败,所以我很好奇。

谢谢,L

doctrine symfony

5
推荐指数
1
解决办法
1274
查看次数

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

我编写了使用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作为参数时,它正在工作(我可以获取存储库并进行一些查找查询).我的错误在哪里?

symfony symfony-3.4

1
推荐指数
1
解决办法
668
查看次数

标签 统计

symfony ×2

doctrine ×1

symfony-3.4 ×1