I'm having troubles getting related entities in a single query.I used both fetch=Eager and manual query with join.Here are my entities
----------------------Store.php--------------------------------------
/**
* @ORM\OneToMany(targetEntity="Mycomp\ProductBundle\Entity\Product", mappedBy="store_id", cascade={"all"},fetch="EXTRA_LAZY")
*
**/
protected $products;
--------------------Product.php---------------------------------------
/**
* @ORM\OneToMany(targetEntity="ProductOption", mappedBy="product", cascade={"all"},orphanRemoval=true,fetch="EAGER")
*
**/
protected $options;
/**
* @ORM\OneToMany(targetEntity="Like", mappedBy="product", cascade={"all"},orphanRemoval=true,indexBy="id",fetch="EAGER")
*
**/
protected $likes;
-------------------Like.php---------------------------------------------
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Product", inversedBy="likes")
*
**/
private $product;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Mycomp\UserBundle\Entity\User",inversedBy="likes")
*
**/
private $user;
-------------------Option.php---------------------------------------------
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="options")
*
**/
private $product;
And the query:
$qb = $em->createQueryBuilder('qb1');
$qb->add('select', 'a', 'p', 'po', 'pl','sl')
->add('from', 'MycompStoreBundle:Store a')
->add('where', 'a.id = :store')
->innerJoin('a.products','p')
->leftJoin('p.options','po') // ***
->leftJoin('p.likes','pl')
->leftJoin('a.followers','sl')// ***
->setParameter('store', $id);
$store = $qb->getQuery()->getResult();
And in twig
{% for product in store.0.products %}
{{ product.name }}
{% for option in product.options %}
{{ option }}
{% endfor %}
<div id="like-holder{{ product.id }}"> {{ like_module(product) }}</div>
{% endfor %}
For 4 products i get these queries:
+ SELECT t0.username AS [...] FROM apps_user t0 WHERE t0.id = ? LIMIT 1
Parameters: [2]
[Display runnable query]
Time: 1.52 ms [ + Explain query ]
+ SELECT s0_.id AS id0, [...] FROM store s0_ INNER JOIN [...] WHERE (s0_.id = ?) AND s0_.dtype [...]
Parameters: ['5']
[Display runnable query]
Time: 0.89 ms [ + Explain query ]
+ SELECT t0.id AS id1, t0.name AS [...] FROM product t0 WHERE t0.store_id_id = ? AND [...]
Parameters: [5]
[Display runnable query]
Time: 0.69 ms [ + Explain query ]
+ SELECT t0.id AS id1, t0.name AS [...] FROM product_option t0 WHERE t0.product_id = ? AND [...]
Parameters: [5]
[Display runnable query]
Time: 0.46 ms [ + Explain query ]
+ SELECT t0.createdAt AS createdAt1, [...] FROM product_like t0 WHERE t0.product_id = ? AND [...]
Parameters: [5]
[Display runnable query]
Time: 0.39 ms [ + Explain query ]
+ SELECT t0.id AS id1, t0.name AS [...] FROM product_option t0 WHERE t0.product_id = ? AND [...]
Parameters: [6]
[Display runnable query]
Time: 0.54 ms [ + Explain query ]
+ SELECT t0.createdAt AS createdAt1, [...] FROM product_like t0 WHERE t0.product_id = ? AND [...]
Parameters: [6]
[Display runnable query]
Time: 0.45 ms [ + Explain query ]
+ SELECT t0.id AS id1, t0.name AS [...] FROM product_option t0 WHERE t0.product_id = ? AND [...]
Parameters: [7]
[Display runnable query]
Time: 0.42 ms [ + Explain query ]
+ SELECT t0.createdAt AS createdAt1, [...] FROM product_like t0 WHERE t0.product_id = ? AND [...]
Parameters: [7]
[Display runnable query]
Time: 0.40 ms [ + Explain query ]
+ SELECT t0.id AS id1, t0.name AS [...] FROM product_option t0 WHERE t0.product_id = ? AND [...]
Parameters: [8]
[Display runnable query]
Time: 0.37 ms [ + Explain query ]
+ SELECT t0.createdAt AS createdAt1, [...] FROM product_like t0 WHERE t0.product_id = ? AND [...]
Parameters: [8]
[Display runnable query]
Time: 0.42 ms [ + Explain query ]
Why is that? As you can see i get addition 2 queries per product.For likes and options.