Symfony2 处理不在关系中的查询
Symfony2 dealing with queries that are not in a relationships
正如标题所说,我需要以某种方式查询以尽可能最好的方式分配不相关的表。使用 dql 可以连接已映射的表,但如果它们未映射,我该如何处理。
例如,我的主索引页面有最多的查询(目前为 71)。据我了解,将来需要查询的项目越多()例如,如果我添加 100 个产品,查询将增加得非常快。
最大的问题是我的索引页面不仅查询一个控制器。例如:
索引动作:
$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,
));
这只是已经查询 objects 分配的索引控制器。但是这些 objects 来自不同的表,除了 newProducts
和 featureProducts
。也许我可以在一个查询中加入他们?但他们必须分开。
不仅如此,在 twig 中我还扩展了更多控制器,它们也有自己的查询。
<span class="top">{% render controller("MpShopBundle:Navbar:navbar" ) %}</span>
{% block sidebar %} {% render url( 'sidebar' ) %} {% endblock %}
所有这些加起来就是查询的分配。减少查询计数的最合乎逻辑的方法是什么?
您发布的控制器进行了大约 5 次查询,加入 $newProducts
和 $featureProducts
的查询不会保存任何内容。您的问题可能出在不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 如何解决该问题的关联上。
正如标题所说,我需要以某种方式查询以尽可能最好的方式分配不相关的表。使用 dql 可以连接已映射的表,但如果它们未映射,我该如何处理。
例如,我的主索引页面有最多的查询(目前为 71)。据我了解,将来需要查询的项目越多()例如,如果我添加 100 个产品,查询将增加得非常快。
最大的问题是我的索引页面不仅查询一个控制器。例如:
索引动作:
$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,
));
这只是已经查询 objects 分配的索引控制器。但是这些 objects 来自不同的表,除了 newProducts
和 featureProducts
。也许我可以在一个查询中加入他们?但他们必须分开。
不仅如此,在 twig 中我还扩展了更多控制器,它们也有自己的查询。
<span class="top">{% render controller("MpShopBundle:Navbar:navbar" ) %}</span>
{% block sidebar %} {% render url( 'sidebar' ) %} {% endblock %}
所有这些加起来就是查询的分配。减少查询计数的最合乎逻辑的方法是什么?
您发布的控制器进行了大约 5 次查询,加入 $newProducts
和 $featureProducts
的查询不会保存任何内容。您的问题可能出在不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