0

I have a following dql in my repostory class:

public function find2($id)
{
    return $this->createQueryBuilder('b')
        ->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
        ->leftJoin('b.branches', 'branch')
        ->leftJoin('branch.productAllocationItems','pai')
        ->leftJoin('pai.DRItems', 'dri')
        ->where('b.id = :id')
        ->setParameter('id', $id)
        ->andWhere('ct > 1')
        ->getQuery()
        ->getResult(); 
}

My problem is COUNT doesnt seem to work, is there any wrong in my query? I just want to return entity whose DRItems based on the condition. Thanks.

I got the following error:

Column not found: 1054 Unknown column 'sclr0' in 'where clause'

0

2 Answers 2

1

Use

->having('ct > 1')

instead of

->andWhere('ct > 1')

public function find2($id)
{
    return $this->createQueryBuilder('b')
        ->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')
        ->leftJoin('b.branches', 'branch')
        ->leftJoin('branch.productAllocationItems','pai')
        ->leftJoin('pai.DRItems', 'dri')
        ->where('b.id = :id')
        ->setParameter('id', $id)
        ->having('ct > 1')
        ->getQuery()
        ->getResult(); 
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can get count by change select statement.

As per documentation select can be use below format

but for count you can achieve with comma separated fields with count , just like we do it in SQL.

// Example - $qb->select('u')
// Example - $qb->select(array('u', 'p'))
// Example - $qb->select($qb->expr()->select('u', 'p'))
public function select($select = null);

Change

->select('b', 'branch', 'pai', 'dri', 'COUNT(dri) AS ct')

To

->select( 'b,branch, pai, dri ,COUNT(dri) AS ct')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.