如何从控制器内部使用 Symfony 2.1 获取 Doctrine 实体的实体管理器 [英] How to get entity manager for Doctrine entity with Symfony 2.1 from inside controller

查看:23
本文介绍了如何从控制器内部使用 Symfony 2.1 获取 Doctrine 实体的实体管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用最新的 Symfony 和 Doctrine 从控制器内部获取实体管理器?

How can I get an entity manager from inside a controller with latest Symfony and Doctrine?

The Book"中描述的方式现已被标记为已弃用.这样做的现代(正确)方法是什么?

The way described in "The Book" flagged as deprecated now. What is a modern (proper) way to do this?

public function someAction()
{
    // getEntityManager() from DoctrineBundleDoctrineBundleRegistry is deprecated
    $entityManager = $this->getDoctrine()->getEntityManager();
    ...
}

推荐答案

使用 $this->getDoctrine()->getManager() 代替.

实际上,最好不要让控制器知道您正在使用的持久层.这些东西应该移到服务层以抽象数据的持久化方式.

Actually, it's best not to make controllers aware of the persistence layer you're using. That stuff should be moved to the Service Layer to abstract the way the data is persisted.

这篇关于如何从控制器内部使用 Symfony 2.1 获取 Doctrine 实体的实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆