I have an entity called School, it has a ManyToMany relation "methods"
class School{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Method", inversedBy="schools")
* @ORM\JoinTable(name="lk_access_method")
* @ORM\OrderBy({"name" = "asc"})
*/
protected $methods;
}
Now I want to write a createQueryBuilder that orders by de count "methods"
Something like:
$schools = $this->createQueryBuilder('s')
->select("s")
->orderBy("COUNT(s.methods)")
->addOrderBy("s.name")
->setMaxResults($count)
->setFirstResult($pos)
->getQuery()
->getResult();
But that didn't work... anybody has a better idea?
Try to add join
->join('s.methods', 'm')
->orderBy("COUNT(m.id)")
EDIT.
->addSelect('COUNT(m.id) as nMethods')
->join('s.methods', 'm')
->groupBy('s.id')
->orderBy("nMethods", 'DESC')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With