Doctrine queryBuilder,返回数组0中的元素

时间:2014-07-15 15:07:52

标签: php json symfony doctrine-orm

所以,我在我的存储库中有这个查询:

    $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();

因此,通过此查询,我有一个帐户列表,按counter排序。当我在json中返回此查询的结果时,我可以看到:

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

你可以在我的json中看到每个元素的"0" ..我怎样才能删除这些0?我想要这样的东西:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

尝试将其更改为

$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();