3

i am working with database data via repository.

I need to get quantity of found rows.

$followers = $em->getRepository('AppBundle:Follower')->findBy(array(
                'user' => $followedUser,
                'follower' => $user
        ));

I have tried to search functions, which return repository find count, but didnt found any.

How should i get the count?

Thank you.

Tigran
  • 633
  • 4
  • 17
  • 26
  • 1
    The `findOneBy` method returns always 1 row. – scoolnico Nov 10 '15 at 18:22
  • 1
    You have to use `findBy` instead. Alternatively you can also do this with a query builder and get an array result. The array count will be your `count(*)`. – Dipen Shah Nov 10 '15 at 18:24
  • yes i have findBy there.. just a typo. – Tigran Nov 10 '15 at 18:27
  • 7
    count($followers) will do the trick. But if all you need is the count and none of the data then try: http://stackoverflow.com/questions/9214471/count-rows-in-doctrine-querybuilder/9215880#9215880 – Cerad Nov 10 '15 at 18:46

2 Answers2

1

Working solution @ 2019

$followersCount = $em->getRepository('AppBundle:Follower')->count([
            'user' => $followedUser,
            'follower' => $user
    ]);
sadiq
  • 2,238
  • 19
  • 21
0

If you want to return all the rows the you can just count($followers) as $followers is a collection that is countable.

If you just want the count and do not want to return a collection of Follower Entities, then you'll need to use createQueryBuilder and select just the count, ensuring to use getSingleScalarResult() to return the count value.

As Cerad notes, check out Count Rows in Doctrine QueryBuilder Which goes into detail of latter option.

SlyDave
  • 986
  • 12
  • 22