How to return only one column

0

Personal I have the following query done.

$ent->createQueryBuilder('t')
                ->select('count(t.id)')
                ->join('t.categoria', 'c')
                ->join('t.noticia', 'n')
                ->groupBy('n.id')
                ->where('n.posicao=1')
                ->andWhere('c.id=16')
                ->andWhere(' t.deleted_at IS NULL OR LENGTH(t.deleted_at) = 0')
                ->getQuery()
                ->getSingleScalarResult();

So she's not just returning me a column with the count. I believe it's because of groupBy. How do I set it to return only the count total without taking out the groupBy. Thanks

    
asked by anonymous 11.05.2015 / 20:16

2 answers

0

Personal I was able to solve using Distinct. It looks like this:

$ent->createQueryBuilder('t')
            ->select('count(DISTINCT c.id)')
            ->join('t.categoria', 'c')
            ->join('t.noticia', 'n')
            ->where('n.posicao=1')
            ->andWhere('c.id=16')
            ->andWhere(' t.deleted_at IS NULL OR LENGTH(t.deleted_at) = 0')
            ->getQuery()
            ->getSingleScalarResult();
    
12.05.2015 / 14:59
0

The query generated by QueryBuilder is wrong, since it is returning not only the result of COUNT(t.id) , but also all columns of t .

Change the above code to:

$ent->createQueryBuilder()
     ->select('count(t.id)')
     ->join('t.categoria', 'c')
     ->join('t.noticia', 'n')
     ->groupBy('n.id')
     ->where('n.posicao=1')
     ->andWhere('c.id=16')
     ->andWhere(' t.deleted_at IS NULL OR LENGTH(t.deleted_at) = 0')
     ->getQuery()
     ->getSingleScalarResult();
    
11.05.2015 / 20:28