Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ObjectManager and EntityManager in Symfony2?

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;     }   } 
like image 867
gremo Avatar asked Apr 23 '12 18:04

gremo


1 Answers

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.

like image 168
Elnur Abdurrakhimov Avatar answered Sep 28 '22 00:09

Elnur Abdurrakhimov