sergeda
sergeda

Reputation: 369

Count many to many relationship items Doctrine, Symfony2

Can somebody help me with this problem. I have two table joined like this:

class Splittest {
     /**
 * @ORM\ManyToMany(targetEntity="Landing")
 * @ORM\JoinTable(name="splittest_landings",
 *      joinColumns={@ORM\JoinColumn(name="splittest_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="landing_id", referencedColumnName="id")}
 *      )
 **/
protected $landings;
}

Now I want to select all splittests with more than one landing. How can I do this in DQL?

Upvotes: 2

Views: 770

Answers (1)

FuzzyTree
FuzzyTree

Reputation: 32392

You'll need to use having to select all splittest with more than 1 landing

SELECT s 
FROM MyBundle\Entity\Splittest
JOIN s.landings l
HAVING COUNT(l) > 1

same in using query builder

$qb->select("s")
    ->from('MyBundle\Entity\Splittest', 's')
    ->innerJoin('s.landings', 'l')
    ->having('count(l) > 1');

Upvotes: 1

Related Questions