Symfony2 中 ObjectManager 和 EntityManager 的区别? [英] Difference between ObjectManager and EntityManager in Symfony2?

查看:11
本文介绍了Symfony2 中 ObjectManager 和 EntityManager 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DoctrineCommonPersistenceObjectManagerDoctrineORMEntityManager 在自定义表单类型中使用时有什么区别?

What's the difference between DoctrineCommonPersistenceObjectManager and DoctrineORMEntityManager when using it in a custom form type?

我可以使用 $this->em->getRepository()$this->om->getRepository() 来获取存储库.

I can get the respository using both $this->em->getRepository() and $this->om->getRepository().

class MyFormType extends SymfonyComponentFormAbstractType
{

    /**
     * @var DoctrineORMEntityManager
     */
    protected $em;

    public function __construct(DoctrineORMEntityManager $em)
    {
        $this->em = $em;
    }

 }

代替:

class MyFormType extends SymfonyComponentFormAbstractType
{

    /**
     * @var DoctrineCommonPersistenceObjectManager
     */
    protected $om;

    public function __construct(DoctrineCommonPersistenceObjectManager $om)
    {
        $this->om = $om;
    }

 }

推荐答案

ObjectManager 是一个接口,EntityManager 是它的 ORM 实现.这不是唯一的实现;例如,来自 MongoDB ODM 的 DocumentManager 也实现了它.ObjectManager 仅提供其所有实现的公共子集.

ObjectManager is an interface and EntityManager is its ORM implementation. It's not the only implementation; for example, DocumentManager from MongoDB ODM implements it as well. ObjectManager provides only the common subset of all its implementations.

如果您希望表单类型与任何 ObjectManager 实现一起使用,请使用它.通过这种方式,您可以从 ORM 切换到 ODM,并且您的类型仍然可以正常工作.但是,如果您需要只有 EntityManager 提供的特定内容并且不打算切换到 ODM,请改用它.

If you want your form type to work with any ObjectManager implementation, then use it. This way you could switch from ORM to ODM and your type would still work the same. But if you need something specific that only EntityManager provides and aren't planning to switch to ODM, use it instead.

这篇关于Symfony2 中 ObjectManager 和 EntityManager 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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