Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Doctrine DBAL to count number of rows from SELECT query

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!

like image 691
penguin Avatar asked Feb 04 '13 17:02

penguin


1 Answers

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();
like image 170
John Linhart Avatar answered Oct 03 '22 03:10

John Linhart