簡體   English   中英

在symfony2中如何將查詢放入json數據?

[英]How in symfony2 put query to json data?

我有要求:

SELECT Bank_ID, Status, COUNT(Bank_ID) FROM int_client_bank WHERE status = 30 or status = 50 or status = 35 or status = 37 GROUP BY Bank_ID, Status;

並查看數據:

"Bank_ID"   "Status"    "COUNT(Bank_ID)"
"1"         "30"        "772"
"1"         "35"        "58"
"1"         "50"        "151"
"2"         "30"        "124"
"2"         "35"        "27"
"2"         "50"        "25"
"3"         "30"        "227"
"3"         "35"        "16"
"3"         "37"        "1"
"3"         "50"        "143"
"4"         "30"        "337"
"4"         "35"        "23"
"4"         "37"        "1"
"4"         "50"        "98"
"5"         "30"        "72"
"5"         "35"        "7"
"5"         "50"        "9"
"6"         "30"        "113"
"6"         "35"        "3"
"6"         "50"        "68"
"7"         "30"        "16"
"7"         "50"        "10"
"8"         "30"        "13"
"8"         "35"        "1"
"8"         "50"        "6"
"9"         "30"        "16"
"9"         "35"        "2"
"9"         "50"        "6"
"10"        "30"        "4"
"10"        "35"        "2"
"11"        "30"        "2"
"11"        "50"        "2"
"12"        "30"        "4"
"12"        "35"        "1"
"12"        "50"        "1"
"13"        "30"        "3"
"13"        "50"        "2"
"14"        "30"        "5"
"15"        "30"        "1"
"15"        "50"        "1"
"16"        "30"        "1"
"17"        "30"        "1"
"18"        "30"        "2"

我如何才能將它放在symfony中以制作JsonResponse ?:

return new JsonResponse(array('data' => $result, 'success' => true));

我需要像這樣的數據:

{
    "data":[
        {"Bank_Id":"1","Status":"30","Count":"772"},
        {"Bank_Id":"1","Status":"35","Count":"58"},
        ...
    ],
    "success":true
}

不清楚您要問的是什么,但是我的猜測是讓symfony根據您的數據創建一個JsonResponse ,如下所示:

use Symfony\Component\HttpFoundation\JsonResponse;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT Bank_ID, Status, COUNT(Bank_ID) FROM int_client_bank WHERE status = 30 or status = 50 or status = 35 or status = 37 GROUP BY Bank_ID, Status');

$bankResult = $query->getResult();
$response = new JsonResponse();
$response->setData(array(
    'data'    => $bankResult,
    'success' => true
));

您需要對數組進行json編碼,然后將其作為json響應發送。

$jsonArray = array(
            'data' => $result,
       'success' => true,
        );

        $response = new Response(json_encode($jsonArray));
        $response->headers->set('Content-Type', 'application/json; charset=utf-8');

        return $response;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM