How to count inner joined table rows in Doctrine query builder or MySQL?

I am trying to figure out how to count instalments attached to the contract and then return it in the result if there are less than 2.

    ->select('cc')
    ->addSelect('COUNT(cci) AS HIDDEN numberOfInstalments')
    ->from('AppBundle:ClientContract', 'cc')
    ->innerJoin('AppBundle:ClientContractInstalment', 'cci', 'WITH', 'cci.clientContract = cc')
    ->andHaving('numberOfInstalments < :numberOfRequiredInstalments'(
    ->setParameter('numberOfRequiredInstalments', 2)
    ->getResult()

The code from above works well when I add one ClientContract, but the problem gets when I add more client contracts, then COUNT(cci) counts all instalments instead of per ClientContract.

How can I change this, so COUNT(cci) will count instalments inside the ClientContract and if less than 2, return in the result?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *