Difference between ObjectManager and EntityManager in Symfony2?

gremo picture gremo · Apr 23, 2012 · Viewed 20.8k times · Source

What's the difference between Doctrine\Common\Persistence\ObjectManager and Doctrine\ORM\EntityManager when using it in a custom form type?

I can get the respository using both $this->em->getRepository() and $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;
    }

 }

Instead of:

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;
    }

 }

Answer

Elnur Abdurrakhimov picture Elnur Abdurrakhimov · Apr 23, 2012

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.

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.