简体   繁体   English

教义相关实体的其他查询

[英]Doctrine related entities additional queries

I am a bit confused as to why Doctrine queries work in this certain way. 对于为什么Doctrine查询以这种特定方式起作用,我有些困惑。 Supposing that we have Articles and Users entities, with Users having a OneToMany relation with Articles. 假设我们有Articles和Users实体,并且Users与Articles具有OneToMany关系。

Then, in the controller we'd get all the articles and send them to a view. 然后,在控制器中,我们将获取所有文章并将其发送到视图。

$em = $this->getEntityManager();    
$articles = $em->getRepository('MyBundle:Article')->findAll();

And in the view we'd loop them. 在视图中,我们将循环它们。

{% for article in articles %}
    {{ article.author.name }}
{% endfor %}

The problem here is that this piece of code does an additional query (for the article's user) for EVERY SINGLE ARTICLE. 这里的问题是,这段代码对每个文章都进行了一个附加查询(针对本文的用户)。

I am aware that we can use DQL, but my question is how does Doctrine work here, why isn't it optimized for this kind of thing, which is pretty common? 我知道我们可以使用DQL,但是我的问题是Doctrine在这里是如何工作的,为什么它没有针对这种事情进行优化(这很常见)? I find this to be a commonly used 'mistake' through applications, that really slows them down. 我发现这是应用程序中经常使用的“错误”,这确实降低了它们的速度。 I've just discovered this and now I have to rewrite so many queries into my controllers. 我刚刚发现了这个问题,现在我不得不将很多查询重写到我的控制器中。

This also defeats the purpose of the ORM, which should actually provide speed in writing the application. 这也违反了ORM的目的,它实际上应该提供编写应用程序的速度。 This forces us to write DQL/QB queries instead of taking advantage of the ORM. 这迫使我们编写DQL / QB查询,而不是利用ORM。 So, when is the ORM a good idea to use if it performs so bad? 那么,如果ORM表现很差,什么时候可以使用它呢?

I am aware that we can use DQL, but my question is how does Doctrine work here, why isn't it optimized for this kind of thing, which is pretty common? 我知道我们可以使用DQL,但是我的问题是Doctrine在这里是如何工作的,为什么它没有针对这种事情进行优化(这很常见)?

Doctrine can't guess what values you will need on the Twig view. 教义不能猜测在Twig视图上需要什么值。

But why doesn't it create the query in the view, when I am calling the related entities? 但是,当我调用相关实体时,为什么不在视图中创建查询呢? Why is it creating separate queries? 为什么要创建单独的查询?

This could not be the solution. 这可能不是解决方案。 See you code: 看到你的代码:

{% for article in articles %}
    {{ article.author.name }}
{% endfor %}

To know what values you will have to display, Symfony should iterate over the loop in order to guess what value you will need, that would be a lot of work before fetching the data. 要知道您将要显示什么值,Symfony应该遍历循环以猜测您将需要什么值,这在获取数据之前需要做很多工作。


You can tell explicitly to Doctrine what table associations should be added to the query: 您可以明确地告诉Doctrine应该将哪些表关联添加到查询中:

In the ArticleRepository repository , you have to join the related table: ArticleRepository 存储库中 ,您必须加入相关表:

<?php

namespace Acme\Bundle\AcmeBundle\Entity;

use Doctrine\ORM\EntityRepository;

class ArticleRepository extends EntityRepository
{
    public function getArticlesWithAuthors()
    {
        $qb = $this->createQueryBuilder('article');

        $query = $qb
            ->select('article, author')
            ->innerJoin('article.author', 'author')
            ->orderBy('a.id', 'DESC')
        ;

        return $query->getQuery()->getResult();
    }
}

Then you can call the method: 然后可以调用该方法:

$articles = $em->getRepository('MyBundle:Article')->getArticlesWithAuthors();

And Doctrine will load the author associated to the article in the same query. 然后,Doctrine将在同一查询中加载与文章关联的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM