OK so I am looking for a neat and short way to count the number of rows from a SELECT query using Doctrine DBAL.
I know that I could SELECT COUNT(*)
but then I need to sort through the array when I fetch results. Alternatively, it's been suggested to look in to getScalarResult()
. But I can't seem to find any documentation about this, other than in DQL (which is a different project).
So what is the neatest way to do this? I guess it's because I'm used to the great MySQLI attribute num_rows
!
I enjoy to use the query builder. An example:
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->select('COUNT(*)');
$queryBuilder->from("the_table");
$queryBuilder->where('some_column = :theValue');
$queryBuilder->setParameter('theValue', $someValue);
return (int) $queryBuilder->execute()->fetchColumn();
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