Difference between ObjectManager and EntityManager in Symfony2?

SymfonyDoctrineDoctrine OrmSymfony Forms

Symfony Problem Overview


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

 }

Symfony Solutions


Solution 1 - Symfony

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongremoView Question on Stackoverflow
Solution 1 - SymfonyElnur AbdurrakhimovView Answer on Stackoverflow