Symfony 2 COUNT Account

In my table "Artiste" there is one column "valideAdmin" which takes the value 1 or 0.

I am trying to do a simple count to return the number of records in my table, where "valideAdmin" is 1:

    $repo = $this   ->getDoctrine()
                    ->getManager()
                    ->getRepository('ProjectMainBundle:Artiste');

    $qb = $repo->createQueryBuilder('valideAdmin');
    $qb->select('COUNT(valideAdmin)');
    $qb->where('valideAdmin=1');

    $count = $qb->getQuery()->getSingleScalarResult();

    return array(
        'count' => $count
    );

But it is always "1" that come back ...

Without the where clause, I have the total number of table entries, but valideAdmin can be 0 or 1. I only need the number count, where valideAdmin = 1

thanks for the help

+4
source share
1 answer

createQueryBuilder() alias, , (: , ).

, (, a Artiste), COUNT() , ( ) valideAdmin :

$repo = $this   ->getDoctrine()
                ->getManager()
                ->getRepository('ProjectMainBundle:Artiste');

$qb = $repo->createQueryBuilder('a');
$qb->select('COUNT(a)');
$qb->where('a.valideAdmin = :valideAdmin');
$qb->setParameter('valideAdmin', 1);

$count = $qb->getQuery()->getSingleScalarResult();

, DQL . DQL SQL .

+17

Source: https://habr.com/ru/post/1542767/


All Articles