对象管理器和EntityManager在Symfony2中的区别? [英] Difference between ObjectManager and EntityManager in Symfony2?
问题描述
Doctrine\Common\Persistence\ObjectManager
和 Doctrine\ORM\EntityManager
在自定义表单类型中使用它时
同时我可以得到相应的代码: $ this-> em-> getRepository ()
和 $ this-> om-> getRepository()
。
< p $ p>
class MyFormType extends \Symfony\Component\Form\AbstractType
{
/ **
* @var Doctrine\\ \\ ORM\EntityManager
* /
protected $ em;
public function __construct(Doctrine\ORM\EntityManager $ em)
{
$ this-> em = $ em;
}
}
而不是:
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;
}
}
ObjectManager
是一个接口, EntityManager
是其ORM实现。这不是唯一的实现;例如,来自MongoDB ODM的 DocumentManager
也实现它。 ObjectManager
仅提供其所有实现的常见子集。
如果希望您的表单类型与任何 ObjectManager
实现,然后使用它。这样,您可以从ORM切换到ODM,您的类型仍然可以正常工作。但是如果您需要特定的东西,只有 EntityManager
提供并且不打算切换到ODM,请改用它。
What's the difference between Doctrine\Common\Persistence\ObjectManager
and Doctrine\ORM\EntityManager
when using it in a custom form type?
With both i can get get the respository: $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;
}
}
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.
这篇关于对象管理器和EntityManager在Symfony2中的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!