Symfony 2/Doctrine: How to lower the num of queries without losing the benefit of ORM?

后端 未结 2 1700
悲&欢浪女
悲&欢浪女 2020-12-21 08:48

I\'m using Symfony 2.7 with Doctrine. My controller actions often look like:

# my/namespace/Controller/ItemsController.php          


        
相关标签:
2条回答
  • 2020-12-21 08:54

    As other's have said, you should fetch the group Entities inside your repository. You can use something like this:

    //inside the repository
    public function findAllFetchGroups(){
    
      return $this->createQueryBuilder('a')
        ->addSelect('gs')->join('a.groups', 'gs')
        ->getQuery()->getResult();
    }
    
    0 讨论(0)
  • 2020-12-21 09:15

    There is two ways to accomplish your target. You can fetch="EAGER" your related data (but it loads always maybe not necessary data) or use query builder to join related collection, it will be loaded in one query. But remember, to keep code clean - no queries outside repositories :)

    0 讨论(0)
提交回复
热议问题