Symfony2中ObjectManager和EntityManager之间的区别?

gre*_*emo 65 doctrine symfony-forms symfony doctrine-orm

什么之间的区别Doctrine\Common\Persistence\ObjectManager,并Doctrine\ORM\EntityManager在自定义表单类型使用的时候吗?

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

class MyFormType extends \Symfony\Component\Form\AbstractType
{

    /**
     * @var Doctrine\ORM\EntityManager
     */
    protected $em;

    public function __construct(Doctrine\ORM\EntityManager $em)
    {
        $this->em = $em;
    }

 }
Run Code Online (Sandbox Code Playgroud)

代替:

class MyFormType extends \Symfony\Component\Form\AbstractType
{

    /**
     * @var Doctrine\Common\Persistence\ObjectManager
     */
    protected $om;

    public function __construct(Doctrine\Common\Persistence\ObjectManager $om)
    {
        $this->om = $om;
    }

 }
Run Code Online (Sandbox Code Playgroud)

Eln*_*mov 103

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

如果您希望表单类型适用于任何ObjectManager实现,请使用它.这样您就可以从ORM切换到ODM,您的类型仍然可以正常工作.但是,如果您需要一些仅EntityManager提供并且不打算切换到ODM的特定内容,请使用它.

  • 我认为现在不推荐使用EntityManager,或者至少在依赖项注入中使用它。但是,如果我是正确的话,仍然还有EntityManagerInterface,而且我不知道Objectmanager和EntityManagerInterface之间的区别。 (3认同)