是否可以使用QueryBuilder管理返回数组的结构?

时间:2019-01-25 11:24:02

标签: php doctrine symfony4 dql doctrine-query

我正在尝试优化我的应用。我想知道我想做的事是否可行。

我根据indexB尝试成功了。

目前,我可以通过PHP更改结果数组的结构。

我想知道QueryBuilder是否能够为我做到这一点,以及如何做到(如果可能)。

这是查询建筑物:

return $this->createQueryBuilder('a')
                        ->select('SUBSTRING(a.creationDate, 1,4) year, TRIM(LEADING \'0\' FROM SUBSTRING(a.creationDate, 6,2)) month, COUNT(a) number')
                        ->where('a.creationDate > :limitDate')
                        ->groupBy('year')
                        ->addGroupBy('month')
                        ->orderBy('year', 'ASC')
                        ->addOrderBy('month', 'ASC')
                        ->setParameter('limitDate', new \DateTime("2000-01-01 00:00:00") )
                        ->getQuery()
                        ->getArrayResult();

这是查询的实际结果:

Array (
    [0] => Array ( [year] => 2016 [month] => 10 [number] => 96 ) 
    [1] => Array ( [year] => 2016 [month] => 11 [number] => 159 ) 
    [2] => Array ( [year] => 2016 [month] => 12 [number] => 118 ) 
    [3] => Array ( [year] => 2016 [month] => 9 [number] => 47 ) 
    [4] => Array ( [year] => 2017 [month] => 1 [number] => 44 ) 
    [5] => Array ( [year] => 2017 [month] => 10 [number] => 47 )
    ...
)

这是我用来更改数组的PHP代码,就像我想要的那样:

$myRestructuredArray = [];
foreach($myArray as $line)
{
    $myRestructuredArray [$line['year']][$line['month']] = $line['number'];
}

这是我需要的结果:

Array (
    [2016] => Array ( 
        [10] => 96 
        [11] => 159 
        [12] => 118 
        [9] => 47 ) 
    [2017] => Array ( 
        [1] => 44 
        [10] => 47 
    ...
)

0 个答案:

没有答案