【问题标题】:How in symfony2 put query to json data?symfony2 中如何对 json 数据进行查询?
【发布时间】:2015-09-03 15:38:19
【问题描述】:

我有要求:

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
}

【问题讨论】:

  • 数字 php 数组转为 JSON 中的 []。关联数组转为 JSON 格式的 {}。您没有向我们展示数据是如何呈现的。除了你为什么不使用实体?

标签: php sql symfony


【解决方案1】:

不清楚你在问什么,但我的猜测是让 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 $bankresult 中?
  • $bankResult is not JSON``JsonResponse()` 自动对您的数组数据进行 json 编码并添加内容类型标头。因此,只需确保 $bankResult 包含您的数据集/查询响应。在示例中添加了一些教义。
  • Tim Dev 我有错误:[语义错误] 第 0 行,'int_client_bank' 附近的第 44 列:错误:未定义类'int_client_bank'。你能改变这个查询请求吗? $em = $this->getDoctrine()->getManager()->getRepository('OmnisoftIntegBundle:IntClientBank')->getQuery .... ?
  • 如果我尝试将查询更改为 SELECT Bank_ID, Status, COUNT(Bank_ID) FROM OmnisoftIntegBundle:IntClientBank WHERE status = 30 or status = 50 or status = 35 or status = 37 GROUP BY Bank_ID, Status [2 /2] QueryException: [Syntax Error] line 0, col 84: Error: Expected end of string, got 'status'
  • 这是您查询中的一个简单错误,我不知道您的数据库。因此,您的查询和创建简单数据数组的任何问题都超出了您上述问题的上下文。请阅读Doctrine - working with Data的手册
【解决方案2】:

您需要对数组进行 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;

【讨论】:

  • 使用 JsonResponse 对象,您不必手动设置标头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多