我在樹枝模板我的類別樹:的Symfony /嫩枝 - 遞歸下降的數據庫查詢
{% for category in categories %}
<li>
<div class="li"><a href="{{ path('accessories_list', { 'category' : category.url }) }}">{{ category.name|trans({}, 'categories')|raw }}</a></div>
{% if category.children is not empty %}
<ul>
{% include "default/_menu_links.html.twig" with {'categories':category.children} only %}
</ul>
{% endif %}
</li>
{% endfor %}
它創建+ - 53個數據庫查詢,如果我有6類,並在每個單一類別7小類。
有沒有辦法減少這個數字? 我在doctrine中使用useResultCache(true),但看起來它不是從緩存中加載(至少不是在開發模式下)。
你如何處理類別樹?
UPDATE: 實體:
...
/**
* One Category has Many Subcategories.
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"persist"}))
*/
private $children;
/**
* Many Subcategories have One Category.
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children", cascade={"persist"})
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
...
/**
* Add child
*
* @param \App\Entity\Product\Category $child
*
* @return Category
*/
public function addChild(\App\Entity\Product\Category $child): Category
{
$this->children[] = $child;
return $this;
}
/**
* Remove child
*
* @param \App\Entity\Product\Category $child
*/
public function removeChild(\App\Entity\Product\Category $child)
{
$this->children->removeElement($child);
}
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren(): Collection
{
return $this->children;
}
/**
* Set parent
*
* @param \App\Entity\Product\Category $parent
*
* @return Category
*/
public function setParent(\App\Entity\Product\Category $parent = null): Category
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return \App\Entity\Product\Category
*/
public function getParent()
{
return $this->parent;
}
...
通常,如果你的數據庫查詢的量取決於所獲取的數據(更多數據=更多的查詢),你應該考慮使用JOIN語句來減少數據庫查詢。如果您可以提供您的學說實體的一些代碼將會很有幫助。 – baris1892
@ radon66嗨,感謝您的回覆。更新我的問題。所以你認爲每天一次在json中生成類別會更好,緩存並在網站上使用? – user8810516