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

查看:233
本文介绍了如何从内部控制器获得带有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 Doctrine\Bundle\DoctrineBundle\Registry 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天全站免登陆