制作一个简单的JSON数组学说查询结果 [英] Making a Doctrine query results with a simple JSON array

查看:148
本文介绍了制作一个简单的JSON数组学说查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有一个名为平面设计的实体。该模式为:

 平面设计类
{
    / **
     * @var整数
     *
     * @ORM \\列(名称=ID,类型=​​整数,可为空= FALSE)
     * @ORM \\标识
     * @ORM \\ GeneratedValue(策略=身份)
     * /
    私人的$ id;    / **
     * @var小数
     *
     * @ORM \\列(名称=指数,键入=十进制,precision = 9,规模= 3,可为空= FALSE)
     * /
    私人$指数;    / **
     * @var日期时间
     *
     * @ORM \\列(名称=日期,键入=日期时间,可为空= FALSE)
     * /
    私人$日期;    / * getter和setter * /

这是一些指数值,根据我的数据库架构(例如):

  ----------------------------------
ID |指数|日期时间|
----------------------------------
1 | 1700.000 |日期时间DATAS |
----------------------------------
2 | 1200.000 |日期时间DATAS |
----------------------------------
3 | 1200.000 |日期时间DATAS |
----------------------------------
4 | 1304.000 |日期时间DATAS |
----------------------------------
等.. |等.. |等.. |

我有这样的方法到控制器:

  $ EM = $这个 - > getDoctrine() -  GT; getManager();
$ queryIndex = $的EM>的createQuery(SELECT g.index
                                    从MySpaceMyBundle:平面设计G');$阵列= array_map(当前,$ queryIndex);$响应=新的响应();
$数据= json_en code($数组);
$响应 - >包头中>设置(Content-Type的,应用/ JSON');
$响应 - > setContent($的数据);返回$反应;

它返回我这个在我的JSON响应:

<$p$p><$c$c>[\"1700.000\",\"1200.000\",\"1200.000\",\"1304.000\",\"1800.000\",\"2012.000\",\"2048.000\",\"1048.000\",\"3000.000\",\"5421.000\"]

但我需要有这个简单的数组的结果(而不是JSON响应我给你的正上方):

<$p$p><$c$c>[1700.000,1200.000,1200.000,1304.000,1800.000,2012.000,2048.000,1048.000,3000.000,5421.000]

我要在我的JSON响应,才能有此十进制值显示它们放入一个highchart图形返回一个简单的数组。

我如何进行?我已经尝试一些教条的方法,如 - &GT; getArrayresult() - &GT; getScalarResult() - &GT;的toArray(),但结果是一样的。我需要让我的查询结果,以一个简单的数组。


解决方案

  json_en code($阵列,JSON_NUMERIC_CHECK);

此解决方案可以帮助你。

In my database I have an entity named Graphique. This the schema for:

class Graphique
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var decimal
     *
     * @ORM\Column(name="index", type="decimal", precision=9, scale=3, nullable=false)
     */
    private $index;

    /**
     * @var datetime
     *
     * @ORM\Column(name="date", type="datetime", nullable=false)
     */
    private $date;

    /*getters and setters*/

This is some values for index, according to my database schema (example):

----------------------------------
id    | index    | dateTime      |
----------------------------------
1     | 1700.000 | dateTime datas|
----------------------------------
2     | 1200.000 | dateTime datas|
----------------------------------
3     | 1200.000 | dateTime datas|
----------------------------------
4     | 1304.000 | dateTime datas|
----------------------------------
etc...| etc...   | etc...        |

I have this method into a controller:

$em=$this->getDoctrine()->getManager();


$queryIndex = $em->createQuery( 'SELECT g.index
                                    FROM MySpaceMyBundle:Graphique g');

$array = array_map('current', $queryIndex);

$response = new Response();
$data = json_encode($array);
$response->headers->set('Content-Type', 'application/json');
$response->setContent($data);

return $response;

it returns me this into my json response:

["1700.000","1200.000","1200.000","1304.000","1800.000","2012.000","2048.000","1048.000","3000.000","5421.000"]

but I need to have this simple array result (instead the json response I give you just above):

[1700.000,1200.000,1200.000,1304.000,1800.000,2012.000,2048.000,1048.000,3000.000,5421.000]

I need to return a simple array in my json response in order to have this decimal values for displaying them into a highchart graphic.

How can I proceed? I already try some Doctrine methods like ->getArrayresult(), ->getScalarResult(), ->toArray(), but the results are the same. I need to make my query result to a simple array.

解决方案

json_encode($array, JSON_NUMERIC_CHECK); 

This solution should help you.

这篇关于制作一个简单的JSON数组学说查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆