Json中的数组与Symfony2

时间:2014-07-29 14:54:45

标签: php arrays json symfony

这就是我想要的:

[{
    "id": 1,
    "targetGroup": {
        "age": [5],
        "gender": [1],
        "income": [2]
    }
    ...
    ,
    {
        ...
    }
}]

我想在我的json回归中找到一个数组。

这是我的Symfony Action:

public function advertisingPartnersAction() {
    $serializer = SerializerBuilder::create()->build();

    $em = $this->getDoctrine()->getRepository('MainBundle:Promotion');
    $query = $em->createQueryBuilder('qbPromotion')
            ->select('qbPromotion.id,qbEntry.entTitle,qbIndustry.indTitle,qbPacking.packTitle,qbEntry.edition,qbEntry.tkp')
            ->join('qbPromotion.entry2', 'qbEntry')
            ->join('qbEntry.packing', 'qbPacking')
            ->join('qbEntry.industry', 'qbIndustry')
            ->getQuery();

    $entity = $query->getResult();

    if ($entity) {
        $jsonEntity = $serializer->serialize($entity, 'json');
    } else {
        // TODO error
        die;
    }

    return new JsonResponse(json_decode($jsonEntity, true));
}

这样可以正常工作并返回:

[
    {"id":2,"entTitle":"Titel","indTitle":"Food","packTitle":"Karton","edition":1,"tkp":"38.10"},
    {"id":3,"entTitle":"Titel","indTitle":"Food","packTitle":"Karton","edition":1,"tkp":"38.10"},
    {"id":1,"entTitle":"Titel 2","indTitle":"Getr\u00e4nke","packTitle":"Flasche","edition":2,"tkp":"38.20"}
]

但我需要一个像我的json中的targetGroup这样的数组。我怎样才能让它发挥作用?

0 个答案:

没有答案