我有一个名为"规划"的sql表。 whchi包含id_place,id_event,date_event。我用symfony2编写了这段代码来发送按地点分组的事件的日期。
$query2 = $em->createQueryBuilder();
$query2->select("p.libPlace , p.idPlace ")
->from('AppBundle:Place', 'p')
->leftJoin('AppBundle:Planning', 'pl', 'WITH', 'pl.idPlace = p.idPlace')
->where('pl.idCevent = :id')
->setParameter('id', $idEvent)
->groupBy('p.idPlace');
$q2 = $query2->getQuery();
$places = $q2->getResult();
$taille = sizeof($places);
$j=0; $a=array();
for ($i=0; $i<= $taille-1; $i++)
{
$query4 = $em->createQueryBuilder();
$query4->select(" z.dateEventPlace dateEventPlace")
->from('AppBundle:Planning', 'z')
->where('z.idCevent = :id')
->andWhere('z.idPlace = :idp')
->setParameter('id', $idEvent)
->setParameter('idp', $places[$i]['idPlace'])
->orderBy('dateEventPlace', 'ASC');
$q4 = $query4->getQuery();
$planning = $q4->getResult();
$p[$j]=$places[$i];
$j++;
$p[$j]=$planning;
$j++;
}return array('places' => $p);
我得到了这个json回复:
places":[
{"libPlace":"Le Colis\u00e9e","idPlace":1},
[{"dateEventPlace":"2017-03-19T08:09:00+0000"},{"dateEventPlace":"2017-03-19T14:19:00+0000"},{"dateEventPlace":"2017-03-24T14:08:00+0000"}],
{"libPlace":"ABC","idPlace":2},
[{"dateEventPlace":"2017-03-22T14:10:00+0000"},{"dateEventPlace":"2017-03-24T16:20:00+0000"}]
]
我希望得到这样的东西:
places":[
{
"libPlace":"Le Colis\u00e9e","idPlace":1,"times":[
{"dateEventPlace":"2017-03-19T08:09:00+0000"},{"dateEventPlace":"2017-03-19T14:19:00+0000"}]
},
{
"libPlace":"ABC","idPlace":2,"times":[
{"dateEventPlace":"2017-03-22T14:10:00+0000"}]
}
]
任何解决方案?