何时在Symfony2中使用实体经理 [英] When to use Entity Manager in Symfony2

查看:117
本文介绍了何时在Symfony2中使用实体经理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在学习如何使用Symfony2。我得到了解释如何使用Doctrine的点。



在这些示例中,他们有时使用实体管理器:

  $ em = $ this-> getDoctrine() - > getEntityManager(); 
$ products = $ em-> getRepository('AcmeStoreBundle:Product')
- > findAllOrderedByName();

,在其他示例中,不使用实体管理器:

  $ product = $ this-> getDoctrine()
- > getRepository('AcmeStoreBundle:Product')
- > find($ ID);

所以我实际上尝试了第一个例子,没有获得实体经理:

  $ repository = $ this-> getDoctrine()
- > getRepository('AcmeStoreBundle:Product');
$ products = $ repository-> findAllOrderedByName();

并得到相同的结果。



那么我什么时候实际需要实体管理员,什么时候可以一次去存储库呢?

解决方案

控制器 getDoctrine()等于 $ this-> get('doctrine'),一个 Symfony\Bundle\DoctrineBundle\Registry 。注册表提供:




  • getEntityManager()返回 Doctrine\ORM\EntityManager ,它又提供 getRepository()

  • getRepository() a href =http://www.doctrine-project.org/api/orm/2.1/class-Doctrine.ORM.EntityRepository.html> Doctrine\ORM\EntityRepository



因此, $ this-> getDoctrine() - > getRepository () $ $ this-> getDoctrine() - > getEntityManager() - > getRepository()



当您想要保留或删除一个实体时,实体经理很有用:

  $ em = $ this-> getDoctrine() - > getEntityManager(); 

$ em-> persist($ myEntity);
$ em-> flush();

如果您只是获取数据,您只能获取存储库:

  $ repository = $ this-> getDoctrine() - > getRepository('AcmeStoreBundle:Product'); 
$ product = $ repository-> find(1);或者更好的是,如果您使用自定义存储库,则将 getRepository()

/ code>在控制器功能中,您可以从IDE获取自动完成功能:

  / ** 
* @return \Acme\HelloBundle\Repository\ProductRepository
* /
protected function getProductRepository()
{
return $这 - > getDoctrine() - > getRepository( 'AcmeHelloBundle:产品');
}


At the moment I am learning how to use Symfony2. I got to the point where they explain how to use Doctrine.

In the examples given they sometimes use the entity manager:

$em = $this->getDoctrine()->getEntityManager();
$products = $em->getRepository('AcmeStoreBundle:Product')
        ->findAllOrderedByName();

and in other examples the entity manager is not used:

$product = $this->getDoctrine()
        ->getRepository('AcmeStoreBundle:Product')
        ->find($id);

So I actually tried the first example without getting the entity manager:

$repository = $this->getDoctrine()
        ->getRepository('AcmeStoreBundle:Product');
$products = $repository->findAllOrderedByName();

and got the same results.

So when do i actually need the entity manager and when is it OK to just go for the repository at once?

解决方案

Looking at Controller getDoctrine() equals to $this->get('doctrine'), an instance of Symfony\Bundle\DoctrineBundle\Registry. Registry provides:

Thus, $this->getDoctrine()->getRepository() equals $this->getDoctrine()->getEntityManager()->getRepository().

Entity manager is useful when you want to persist or remove an entity:

$em = $this->getDoctrine()->getEntityManager();

$em->persist($myEntity);
$em->flush();

If you are just fetching data, you can get only the repository:

$repository = $this->getDoctrine()->getRepository('AcmeStoreBundle:Product');
$product    = $repository->find(1);

Or better, if you are using custom repositories, wrap getRepository() in a controller function as you can get auto-completition feature from your IDE:

/**
 * @return \Acme\HelloBundle\Repository\ProductRepository
 */
protected function getProductRepository()
{
    return $this->getDoctrine()->getRepository('AcmeHelloBundle:Product');
}

这篇关于何时在Symfony2中使用实体经理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆