Symfony2处理不在关系中的查询

时间:2022-10-22 17:08:24

As the title says I need to somehow query for allot of tables that are not related in the best way possible. With dql you can join tables that are mapped, but if they are not mapped, how can I deal with that.

正如标题所说,我需要以某种方式查询以最佳方式无关的所有表格。使用dql,您可以连接映射的表,但如果它们未映射,我该如何处理。

For example my main index page has the most queries(71 for now). As I understand in the future the more items it will have to query()for example if I add 100 products the query will increase very fast.

例如,我的主索引页面查询最多(现在为71)。据我所知,将来查询的项目越多(例如,如果我添加100个产品,查询将会非常快速地增加。

The biggest problem is that my index page not only queries form one controller. For example:

最大的问题是我的索引页面不仅查询一个控制器。例如:

indexAction:

$em = $this->getDoctrine()->getManager();
            $carousel = $em->getRepository('ApplicationSonataMediaBundle:Gallery')->findOneBy(array('name' => 'Carousel'));
            $featureProducts = $em->getRepository('MpShopBundle:Product')->findBy(array('status' => 1, 'special' => 1));
            $newProducts = $em->getRepository('MpShopBundle:Product')->findBy(array('status' => 1), array('id' => 'ASC'), 8); // pakeisti y DESC
            $session = $this->getRequest()->getSession();
            $skin = $em->getRepository('MpShopBundle:Skin')->findOneBy(array('status' => 1));

               return $this->render('MpShopBundle:Frontend:index.html.twig',  array(
               'featureProducts'=>$featureProducts,
               'skin' => $skin,
               'newProducts' => $newProducts,
               'carousel' => $carousel,
               ));

This is only the index controller that already queries for allot of objects. But these objects are from different tables except the newProducts and featureProducts. Maybe I can join them to in one query? But they havo to be seperate.

这只是已经查询了所有对象的索引控制器。但是这些对象来自不同的表,除了newProducts和featureProducts。也许我可以在一个查询中加入他们?但他们仍然需要分开。

Not only that but in the twig I extend even more controllers that have their own queries too.

不仅如此,我还扩展了更多有自己查询的控制器。

<span class="top">{% render controller("MpShopBundle:Navbar:navbar" ) %}</span>

{% block sidebar %} {% render url( 'sidebar' ) %} {% endblock %}

This all sums up to allot of queries.. What would be the most logical way to reduce the query count?

这一切总结为分配查询..减少查询计数最合理的方法是什么?

1 个解决方案

#1


1  

The controller you posted makes around 5 queries, and joining the queries for $newProducts and $featureProducts won't save anything. Your problem probably lies with associations that are not Fetch joined, i.e: your product might have some association to some other entity and when you iterate through your products in your template and want to fetch the associated entity, it will create another query to the database to load that entity, since they are lazy loaded. See this question on how to fix that.

您发布的控制器大约有5个查询,加入$ newProducts和$ featureProducts的查询将不会保存任何内容。您的问题可能在于未加入Fetch的关联,即:您的产品可能与某个其他实体有某种关联,当您在模板中迭代产品并想要获取关联实体时,它将创建另一个查询到数据库加载该实体,因为它们是延迟加载的。请参阅此问题以了解如何解决此问题。

#1


1  

The controller you posted makes around 5 queries, and joining the queries for $newProducts and $featureProducts won't save anything. Your problem probably lies with associations that are not Fetch joined, i.e: your product might have some association to some other entity and when you iterate through your products in your template and want to fetch the associated entity, it will create another query to the database to load that entity, since they are lazy loaded. See this question on how to fix that.

您发布的控制器大约有5个查询,加入$ newProducts和$ featureProducts的查询将不会保存任何内容。您的问题可能在于未加入Fetch的关联,即:您的产品可能与某个其他实体有某种关联,当您在模板中迭代产品并想要获取关联实体时,它将创建另一个查询到数据库加载该实体,因为它们是延迟加载的。请参阅此问题以了解如何解决此问题。