I am trying to get a count, using this query:
$count = $entityManager->getRepository('Model:Machine')
->createQueryBuilder('m')
->select('COUNT(m.id)')
->where('m.number = :number')
->setParameter('number', $value)
->getQuery()
->getSingleScalarResult();
If I use a $value
that I know exists, then $count
returns as 1
. If I use a value that I know does NOT exist, then $count
is empty.
Why isn't it set to 0
?
Thanks
getSingleScalarResult works different that you might think. It expects to get a simple data from query and cast it to an integer. When query couldn't get machines with number equal to parameter, there are no results, so results variable is empty.