Symfony在某些类中使用entityManager

时间:2019-01-15 15:03:20

标签: symfony doctrine

如何在Symfony的某些类中获取EntityManager? 试图添加服务,但entityManager为空... Symfony 4中执行此操作的最佳方法是什么?

用于从另一个类中调用存储库中的函数。

1 个答案:

答案 0 :(得分:3)

要访问服务内部的entityManager,必须首先构造它。

namespace App\Service;

use Doctrine\ORM\EntityManagerInterface;

class ServiceName
{
    private $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }
    public function myFunction()
    {
        $classRepo = $this->entityManager->getRepository(ClassName::class);
    }
}
相关问题