Php 在symfony2中,如何将查询放入json数据?
我要求:Php 在symfony2中,如何将查询放入json数据?,php,sql,symfony,Php,Sql,Symfony,我要求: 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"
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根据你的数据做一个a,如下所示:
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;
但我怎样才能提出质疑呢?要将其放入json$bankresult中?
$bankresult
不是json``JsonResponse
()`自动对数组数据进行json编码并添加内容类型头。因此,只需确保$bankResult
包含您的数据集/查询响应。在示例中添加了一些原则。Tim Dev i have error:[语义错误]第0行,第44列,靠近“int_client_bank”:错误:未定义类“int_client_bank”。您可以更改此查询请求吗$em=$this->getDoctrine()->getManager()->getRepository('OmniSoftIntegratBundle:IntClientBank')->getQuery?如果我尝试更改查询以从OmniSoftIntegrabundle中选择银行ID、状态、计数(银行ID):IntClientBank,其中状态=30或状态=50或状态=35或状态=37按银行ID分组,状态[2/2]查询异常:[语法错误]第0行,第84列:错误:字符串预期结束,得到“状态”,这是查询中的一个简单错误,我不知道您的数据库。因此,任何查询和创建简单数据数组的问题都与上述问题无关。请阅读使用JsonResponse对象的手册,您不必手动设置标题。数字php数组在JSON中变成[]。在JSON中,关联数组变为{}。您没有向我们展示数据是如何呈现的。此外,为什么不使用实体呢?