I'm trying to optimize my app. I need to know if what I wan to do is possible or not.
I made some try based on indexBy whitout success.
Currentely I am able to change the structure of the results array like I need via PHP.
I wan to know if QueryBuilder is able to make that for me and how (if possible).
Here is the query building :
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();
Here is the actual result of the query :
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 )
...
)
Here is PHP code I use to change the array like I wan :
$myRestructuredArray = [];
foreach($myArray as $line)
{
$myRestructuredArray [$line['year']][$line['month']] = $line['number'];
}
Here is the result I need :
Array (
[2016] => Array (
[10] => 96
[11] => 159
[12] => 118
[9] => 47 )
[2017] => Array (
[1] => 44
[10] => 47
...
)