Difference between ObjectManager and EntityManager in Symfony2?

22,803

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.

Share:
22,803
gremo
Author by

gremo

Updated on July 05, 2022

Comments

  • gremo
    gremo almost 2 years

    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;
        }
    
     }
    
  • Mat
    Mat over 6 years
    I think the use of EntityManager is now deprecated, or at least its use in a dependency injection. But there's still EntityManagerInterface if I'm correct, and I don't know the difference between Objectmanager and EntityManagerInterface.