2017-09-26 56 views
2

我想问你,如果可能的话,在本地教条方法preload子属性中创建。不通过延迟加载。例如: 我有3个实体。 (产品,项目,颜色)。PHP主义通过一个SQL查询加载3个实体

产品在项目上具有ManyToMany关系,项目在Color上具有ManyToMany关系。

/** 
* @ORM\Entity 
* @ORM\Table(name="product") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository") 
*/ 
class Product 
{ 
    /** 
    * @var ArrayCollection 
    * 
    * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Item", inversedBy="products") 
    * @ORM\JoinTable(
    *  name="product_item", 
    *  joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false)}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id", nullable=false)} 
    *) 
    */ 
private $items; 

-

/** 
    * @ORM\Entity 
    * @ORM\Table(name="item") 
    * @ORM\Entity(repositoryClass="AppBundle\Repository\ItemRepository") 
    */ 
    class Item 
{ 
    /** 
    * @var ArrayCollection 
    * 
    * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Product", mappedBy="items") 
    */ 
    private $products; 

    /** 
    * @var ArrayCollection 
    * 
    * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Colour", inversedBy="products") 
    * @ORM\JoinTable(
    *  name="item_colour", 
    *  joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id", nullable=false)}, 
    *  inverseJoinColumns={@ORM\JoinColumn(name="colour_id", referencedColumnName="id", nullable=false)} 
    *) 
    */ 
    private $colours; 

-

/** 
    * @ORM\Entity 
    * @ORM\Table(name="colour") 
    * @ORM\Entity(repositoryClass="AppBundle\Repository\ColourRepository") 
    */ 
    class Colour 
{ 
    /** 
    * @var ArrayCollection 
    * 
    * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Item", mappedBy="colours") 
    */ 
    private $items; 

,我有这样的代码:

$products = $productRepository->findAll(); 

foreach ($products as $product){ 
    $items = $product->getItems(); 

    foreach ($items as $item){ 
     // Do some 
    } 
} 

我可以使用的东西只有一个SQL到DB?

感谢

编辑:

$query = $this->_em->createQueryBuilder() 
      ->select('p') 
      ->from(Product::class, 'p') 
      ->leftJoin('p.discount', 'discount') 
      ->leftJoin('p.productLanguages', 'productLanguages') 
      ->leftJoin('p.combinationsInfo', 'combinationsInfo') 
      ->leftJoin('p.tableSize', 'tableSize') 
      ->leftJoin('p.state', 'state') 
      ->leftJoin('p.tags', 'tags') 
      ->leftJoin('p.categories', 'categories') 
      ->leftJoin('p.images', 'images') 
      ->leftJoin('p.similarProducts', 'similarProducts') 
      ->leftJoin('p.similarProductsReverse', 'similarProductsReverse') 
      ->leftJoin('p.partnerProducts', 'partnerProducts') 
      ->leftJoin('p.partnerProductsReverse', 'partnerProductsReverse') 
      ->leftJoin('p.mainCategory', 'mainCategory') 
      ->leftJoin('p.variants', 'variants') 
      ->leftJoin('p.type', 'type') 
      ->leftJoin('p.sex', 'sex') 

      ->leftJoin('discount.discountLanguages', 'discountDiscountLanguages') 

      ->leftJoin('productLanguages.tax', 'productLanguagesTax') 
      ->leftJoin('productLanguages.currency', 'productLanguagesCurrency') 
      ->leftJoin('productLanguages.language', 'productLanguagesLanguage') 

      ->leftJoin('images.categories', 'imagesCategories') 
      ->leftJoin('images.widgets', 'imagesWidgets') 
      ->leftJoin('images.reporters', 'imagesReporters') 

      ->leftJoin('categories.categoryLanguages', 'categoriesCategoryLanguages') 
      ->leftJoin('categories.type', 'categoriesType') 
      ->leftJoin('categories.images', 'categoriesImages') 
      ->leftJoin('categories.reporters', 'categoriesReporters') 
      ->leftJoin('categories.parent', 'categoriesParent') 
      ->leftJoin('categories.descendants', 'categoriesDescendants') 

      ->leftJoin('categoriesDescendants.categoryLanguages', 'categoriesDescendantsCategoryLanguages') 
      ->leftJoin('categoriesDescendants.type', 'categoriesDescendantsType') 
      ->leftJoin('categoriesDescendants.images', 'categoriesDescendantsImages') 
      ->leftJoin('categoriesDescendants.reporters', 'categoriesDescendantsReporters') 
      ->leftJoin('categoriesDescendants.parent', 'categoriesDescendantsParent') 
      ->leftJoin('categoriesDescendants.descendants', 'categoriesDescendantsDescendants') 

      ->leftJoin('mainCategory.categoryLanguages', 'mainCategoryCategoryLanguages') 
      ->leftJoin('mainCategory.type', 'mainCategoryType') 
      ->leftJoin('mainCategory.images', 'mainCategoryImages') 
      ->leftJoin('mainCategory.reporters', 'mainCategoryReporters') 
      ->leftJoin('mainCategory.parent', 'mainCategoryParent') 
      ->leftJoin('mainCategory.descendants', 'mainCategoryDescendants'); 
+0

您在Doctrine上使用哪个版本? – Nico

+0

我使用symfony 3,所以教条2(我认为) –

回答

1

是的,你可以使用以下DQL得到的结果

SELECT p,i 
FROM Product p 
JOIN p.items i /* or LEFT JOIN to get all products */ 
+0

好吧,如果我有巨大的实体,我想加载3层深,所以我必须使用这个?即编辑问题。 –

+0

是的,我看到您更新的帖子。你的查询生成器也会生成单个的sql和DQL,你可以通过执行$ query-> getSQL()或$ query-> getDQL()来打印。 –