Doctrine2(Doctrine 2.1)渴望加载Symfony2 [英] Doctrine2 (Doctrine 2.1) eager loading in Symfony2

查看:172
本文介绍了Doctrine2(Doctrine 2.1)渴望加载Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的Symfony2项目中有两个实体:类别文章(有很多文章的类别)



在我的 CategoryRepository 中,我有这种方法:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
b b b b b b b b b b b b b) $ b - > getQuery() - > getResult();
}

如果我记得很好,在Symfony1.4(和相应版本的Doctrine ),返回的对象将通过相应的文章对象填充其文章属性。
现在,在Symfony2中,返回代理对象。



所以如果我循环访问一个特定类别的文章,就像执行迭代一样多的查询。 ($ category-> getArticles()as $ article){
echo $ article-> getDoctrine()
- > getRepository('')getTitle();
}

我明白这是Doctrine2.1的默认懒惰加载行为。



问题1:这是一个更好的解决方案?
N个查询而不是1。



我尝试通过执行以下操作强制加载:

  findAllDummy(){
return $ this-> createQueryBuilder('c')
- > leftJoin('c.articles a')
- > getQuery()
- > setFetchMode('Category','articles','EAGER')
- > getResult();
}

但结果保持不变。



问题2:如何强制加载Doctrine2?

解决方案

你加入一张桌子,但你没有从中选择任何东西。将 - > addSelect('a')添加到查询构建器。考虑两个以下SQL查询来了解差异:

  SELECT a.id,a.title 
FROM article a
JOIN类别c ON a.category_id = c.id
WHERE a.id = 123;

SELECT a.id,a.title,c.id,c.name
FROM article a
JOIN类别c ON a.category_id = c.id
WHERE a.id = 123;






渴望/懒惰加入与DQL无关查询。它定义了当您使用 $ articleRepository-> find(123)时应加载的内容。


Let's say I have two entities in my Symfony2 project : Category and Article (a category having many articles).

In my CategoryRepository, I have this method:

findAllDummy(){
  return $this->createQueryBuilder('c')
              ->leftJoin('c.Articles a')
              ->getQuery()->getResult();
}

If I remember well, in Symfony1.4 (and the corresponding version of Doctrine), the returned objects would have their 'articles' attribute filled by the corresponding Article objects. Now, in Symfony2, Proxy objects are returned.

So if I loop through a specific category's articles, As many queries as iterations will be executed.

foreach($category->getArticles() as $article){
  echo $article->getDoctrine()
               ->getRepository('')getTitle();
}

I understand this is Doctrine2.1's default lazy loading behavior.

Question 1: how is this a better solution? N queries instead of 1.

I tried to force eager loading by doing the following:

findAllDummy(){
  return $this->createQueryBuilder('c')
              ->leftJoin('c.articles a')
              ->getQuery()
              ->setFetchMode('Category', 'articles', 'EAGER')
              ->getResult();
}

But the result remains the same.

Question 2: how to force eager loading in Doctrine2?

解决方案

You're joining a table but you're not selecting anything from it. Add ->addSelect('a') to your query builder. Consider two following SQL queries to understand the difference:

SELECT a.id, a.title
FROM article a 
JOIN category c ON a.category_id = c.id 
WHERE a.id = 123;

SELECT a.id, a.title, c.id, c.name 
FROM article a 
JOIN category c ON a.category_id = c.id 
WHERE a.id = 123;


Eager/lazy joining has nothing to do with DQL queries. It defines what should be loaded when you use $articleRepository->find(123).

这篇关于Doctrine2(Doctrine 2.1)渴望加载Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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