2017-11-24 11 views
0

下位の1から多数のクエリを下げる方法はありますか?Symfony/Doctrine - OneToManyクエリが多すぎます

私は1対多のエンティティ(カテゴリツリー)を自己参照しています。ここで

private function getChildren(string $category): array 
{ 
    $em = $this->getDoctrine(); 
    $category = $em->getRepository(Category::class)->getByUrl($category); 
    $children = $category->getChildren(); 
    $childrenArr = []; 
    if (!empty($children)) { 
     foreach ($children as $child) { 
      $childrenArr[] = $child->getId(); 
      if (!empty($child->getChildren())) { 
       $this->getChildren($child->getUrl(), $childrenArr); 
      } 
     } 
    } 
    return $childrenArr; 
} 

は私のリポジトリです:

public function getByUrl(string $url) 
{ 
    $qb = $this->createQueryBuilder('c'); 
    $qb->where('c.url = :url'); 
    $qb->setParameter('url', $url); 
    return $qb->getQuery()->useQueryCache(true)->getSingleResult(); 
} 

問題は、それはばかげて...それは5人の子供を見つけた場合、それは57個のクエリを作成し、ある私はこのようなすべての子どもたちを取得しようとしています。

UPDATE:

エンティティ構造:

使用し
/** 
    * One Category has Many Subcategories. 
    * @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"persist"}, fetch="EAGER")) 
    */ 
    private $children; 
    /** 
    * Many Subcategories have One Category. 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="children", cascade={"persist"}, fetch="EAGER") 
    * @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; 
    } 

    /** 
    * Add products 
    * 
    * @param \App\Entity\Product\Product $products 
    * 
    * @return Category 
    */ 
    public function addProducts(\App\Entity\Product\Product $products) 
    { 
     $this->products[] = $products; 

     return $this; 
    } 

    /** 
    * Remove products 
    * 
    * @param \App\Entity\Product\Product $products 
    */ 
    public function removeProducts(\App\Entity\Product\Product $products) 
    { 
     $this->products->removeElement($products); 
    } 

    /** 
    * Get products 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getProducts() 
    { 
     return $this->products; 
    } 

が、それは、私はすべての子供を要求したとき、あまりにも多くのクエリを避けるために2つのクエリ(現在、55 57だった)

+0

?また、すでにクエリビルダを使用しているので、それを使用して5人の子を取得するクエリを作成することをおすすめします。 – Confidence

+0

@Confidenceこんにちは、私は私の質問を更新しました。私は子供がどれくらいいるか分かりません。0または5または10を持つことができるので、単にクエリを作成することはできません。または私はあなたを誤解しましたか?おかげで – user8810516

答えて

0

を削除= EAGERフェッチ私は自分のエンティティでfetch = "EAGER"を実行しますが、それは自動的に子テーブルからinnerJoinを行います。

例:あなたのエンティティがどのように構成されるか

/** 
* @ORM\OneToMany(targetEntity="CoreBundle\Entity\Category", mappedBy="parent", cascade={"persist", "remove"}, fetch="EAGER") 
*/ 
private $children; 
+0

ありがとう、これを使用、今は55の代わりに57のクエリ – user8810516

関連する問題