我将 Symfony2 与 Doctrine2 一起使用。对于我的项目,我制作了具有不同关联映射的实体。首先,我确实看到了大约 7 个请求一个对象的查询,所以我决定进行“急切加载”并将它减少到三个查询。
但是其中两个在symfony工具栏(Profiler)中看起来是一样的,直接相互调用。据我了解,我的代码中不需要第三个查询。
那么我必须在哪里设置我的断点在教义 php 文件中,以查看我的代码的哪一行使教义调用新查询?还是有另一种解决方案可以查看我如何优化此请求?
更新:
在考虑了 Artworkad 的答案之后,我必须更详细地进行说明。这是因为我没有通过我的控制器发出 2 个对象请求。但也许它与我的树枝有关?
我的控制器
public function gebietAction($gebiet){
$em = $this->getDoctrine()->getEntityManager();
/* @var $gebietobj Gebiet */
$gebietobj = $em->getRepository('ACGSigwxBundle:Gebiet')->findOneBy(array('short' => $gebiet));
if (!$gebietobj) {
throw $this->createNotFoundException('Kann das angegebene Gebiet nicht finden!');
}
return $this->render('ACGSigwxBundle:Sigwx:sigwx.html.twig',array("gebiet"=>$gebietobj));
}
我的树枝模板
{% extends "ACGSigwxBundle::layout.html.twig" %}
{% block content %}
<h1>{{ gebiet.getName() }}</h1>
<p>My sectors:</p>
<ul>
{% for gs in gebiet.getGebietssektoren() %}
<li>{{ gs.getSektor().getName() }}</li>
{% endfor %}
</ul>
{% endblock %}
对象关联
Gebiet n:n Sektor
与属性有关联。所以我Gebiet 1:n Gebietsektoren n:1 Sektor
用标准的[doctrine2关联映射(http://docs.doctrine-project.org/en/latest/reference/association-mapping.html)ManyToOne
和OneToMany
我从探查器中列出的 3 个查询
SELECT t0.id AS id1, t0.name AS name2, t0.short AS short3, t0.parent_id AS parent_id4 FROM gebiet t0 WHERE t0.short = ? LIMIT 1 Parameters: [app]
SELECT t0.id AS id1, t0.position AS position2, t0.size AS size3, t0.gebiet_id AS gebiet_id4, t0.sektor_id AS sektor_id5, t6.id AS id7, t6.name AS name8, t6.typ AS typ9, t6.erweitert AS erweitert10, t6.sortorder AS sortorder11 FROM gebietssektoren t0 INNER JOIN sektor t6 ON t0.sektor_id = t6.id WHERE t0.gebiet_id = ? Parameters: [1]
SELECT t0.id AS id1, t0.position AS position2, t0.size AS size3, t0.gebiet_id AS gebiet_id4, t0.sektor_id AS sektor_id5, t6.id AS id7, t6.name AS name8, t6.typ AS typ9, t6.erweitert AS erweitert10, t6.sortorder AS sortorder11 FROM gebietssektoren t0 INNER JOIN sektor t6 ON t0.sektor_id = t6.id WHERE t0.gebiet_id = ? Parameters: [1]