我可以在实体中使用 getDoctrine 和 getManager 吗?我很抱歉,但我不明白这是如何工作的

Cod*_*tiu 1 doctrine symfony sonata-admin

我想在实体中使用 getDoctrine 和 getManager 函数。这可能吗?或者有什么方法可以解决这个问题?我想在这样的数据库中插入一些东西:

$history = new Policy();
        $history->setName($file1->getClientOriginalName());
        $history->setPolicyNumber($this->getPolicyNumber());
        $history->setOrderId($this->getOrderId());
        $history->setPath($this->getPathFile1());
        $history->setDocumentType($this->getDocument1Type());
        $history->setPrintAction($this);

        $em = $this->getDoctrine()->getManager();
        $em->persist($history);
        $em->flush();
Run Code Online (Sandbox Code Playgroud)

Fab*_*ARD 5

使用 Doctrine ORM,实体具有独特的作用:数据容器!根据 Doctrine 架构,没有理由在内部注入 EntityManager。

如果您需要这样做,您正在尝试将业务层的一些代码放入层中。因此,尝试将您的代码移动到服务中,例如您的实体的管理器,或者如果您在控制器中懒惰,但它有点蹩脚。