phpsymfonydoctrine-ormdoctrinesymfony4

Generate query on doctrine querybuilder


I have three entities Compound, ComponentQuantity, and Component the Compound has a ManyToMany relationship with ComponentQuantity and ComponentQuantity has a ManyToMany relationship with Component.

I need to get the Compound's which ComponentQuantity are related with a Component,

If I would need the component quantity I would use:

$qb = $this->createQueryBuilder("c")
            ->where(':componentQuantity MEMBER OF c.componentQuantities')
            ->setParameters(array('componentQuantity' => $componentQuantity));

but I need something like

$qb = $this->createQueryBuilder("c")
            ->where(':component MEMBER OF c.componentQuantities.components')
            ->setParameters(array('component' => $component));

but this not work.


Solution

  • You can do MEMBER OF on a level so for example c.componentQuantities but not c.componentQuantities.components. To do this you would need to do this (make a join to get the data at the right level):

    $qb = $this->createQueryBuilder("c")
               ->join('c.componentQuantities', 'cq')
               ->where(':component MEMBER OF cq.components')
               ->setParameters(array('component' => $component));