简体   繁体   中英

Doctrine queryBuilder, return element in array 0

So, i have this query in my repository :

    $qb = $this->_em->createQueryBuilder();
    $qb->select(array('a', 'COUNT(t.id) as counter'))
            ->from('MyProjectBundle:Account', 'a')
                ->leftjoin('a.tha', 'th')
                ->leftjoin('th.the', 't')
            ->where('t.is_good = ?1')
            ->groupby('a')
            ->orderby('counter', 'DESC')
            ->setMaxResults(30)
            ->setParameters(array(1 => $is_good));
    return $qb->getQuery()->getResult();

So, with this query, i have a list of account, order by counter . When i return the result of this query in json, i can see :

{    
    "myjson": [
        {
            "0": {
                "username": "blabla",
            },
            "counter": "2"
        }
        {
            "0": {
                "username": "aeiouy",
            },
            "counter": "1"
        }
    ]
}

You can see the "0" for each element in my json.. How can i remove these 0 ? I want something like :

{    
    "myjson": [
        {
            "username": "blabla",                
            "counter": "2"
        },
        {
            "username": "aeiouy",
            "counter": "1"
        }
    ]
}

Any ideas ?

Try to change it to

$qb = $this->_em->createQueryBuilder();
$qb->select(array('a.username', 'COUNT(t.id) as counter'))
        ->from('MyProjectBundle:Account', 'a')
            ->leftjoin('a.tha', 'th')
            ->leftjoin('th.the', 't')
        ->where('t.is_good = ?1')
        ->groupby('a')
        ->orderby('counter', 'DESC')
        ->setMaxResults(30)
        ->setParameters(array(1 => $is_good));
return $qb->getQuery()->getResult();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM