我有一个带有条令查询的控制器:
$em=$this->getDoctrine()->getManager();
$queryIndex = $em->createQuery( 'SELECT g.index
FROM MySpaceMyBundle:Graphique g');
$result = $queryIndex->getScalarResult();
$resultArray = $result -> toArray();
$response = new Response();
$data = json_encode($resultArray);
$response->headers->set('Content-Type', 'application/json');
$response->setContent($data);
return $response;
但我有一个错误:
错误:在非对象500上调用成员函数toArray()内部服务器错误-致命错误异常
但如果我在控制器中尝试此操作,它会起作用:$array=数组(1,5,7,8,74,24,9,6,5,48555);
$response = new Response();
$data = json_encode($array);
$response->headers->set('Content-Type', 'application/json');
$response->setContent($data);
return $response;
它返回给我:[1,5,7,85,74,24,9,6,5,4,8555]
我只需要将查询结果呈现到数组中,就可以将数据值传递到用于Highcharts的json中。
更新
如果我在页面上查看json结果S(请参阅下面的controlelr),我的查询结果是:
[{"index":"1700.000"},{"index":"1200.000"},{"index":"1200.000"},{"index":"1304.000"},{"index":"1800.000"},{"index":"2012.000"},{"index":"2048.000"},{"index":"1048.000"},{"index":"3000.000"},{"index":"5421.000"}]
index是数据库中列的名称,数字为数据库中索引的值。
如果我做了一个var_dump
,这就是结果:
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=10)</i>
0 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1700.000'</font> <i>(length=8)</i>
1 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1200.000'</font> <i>(length=8)</i>
2 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1200.000'</font> <i>(length=8)</i>
3 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1304.000'</font> <i>(length=8)</i>
4 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1800.000'</font> <i>(length=8)</i>
5 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2012.000'</font> <i>(length=8)</i>
6 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2048.000'</font> <i>(length=8)</i>
7 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1048.000'</font> <i>(length=8)</i>
8 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'3000.000'</font> <i>(length=8)</i>
9 <font color='#888a85'>=></font>
<b>array</b> <i>(size=1)</i>
'index' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'5421.000'</font> <i>(length=8)</i>
</pre>
正如我所说,这些结果是为了在highchart.js
的图形中显示出来的。这是我认为的脚本:
$(document).ready(function() {
var options = {
chart: {
renderTo: 'linechart',
type: 'spline'
},
series: [{}]
};
var url = "{{ path('myPage') }}";
$.getJSON(url, function(data) {
options.series[0].data = data;
var chart = new Highcharts.Chart(options);
});
});
highchart有一个简单的数组来显示图形中的结果,因为当我在控制器中使用以下代码进行测试时,结果显示正确:
$array = array(1,5,7,85,74,24,9,6,5,4,8555);
$response = new Response();
$data = json_encode($array);
$response->headers->set('Content-Type', 'application/json');
$response->setContent($data);
return $response;
它返回给我:
/*var_dump*/
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=11)</i>
0 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>1</font>
1 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>5</font>
2 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>7</font>
3 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>85</font>
4 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>74</font>
5 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>24</font>
6 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>9</font>
7 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>6</font>
8 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>5</font>
9 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>4</font>
10 <font color='#888a85'>=></font> <small>int</small> <font color='#4e9a06'>8555</font>
</pre>
/*json response*/
[1,5,7,85,74,24,9,6,5,4,8555]
代替
[{"index":"1700.000"},{"index":"1200.000"},{"index":"1200.000"},{"index":"1304.000"},{"index":"1800.000"},{"index":"2012.000"},{"index":"2048.000"},{"index":"1048.000"},{"index":"3000.000"},{"index":"5421.000"}]
我想我需要从我的数据库中得到这样的结果:
[1700.000, 1200.000,1200.000,1304.000,1800.000,2012.000,2048.000,1048.000,3000.000,5421.000]
事实上,问题出在数据上。事实上,我看到了两个问题:index
对Highcharts来说毫无意义。应该有y
。此外,您的值是字符串,而应该是数字。
或者,我认为简单的预处理就足够了:
$.getJSON(url, function(data) {
var d = [];
$.each(data, function(i, e) {
d.push(parseFloat(e.index)); // create format [y_0, y_1, ... ,y_N]
});
options.series[0].data = d;
var chart = new Highcharts.Chart(options);
});
来自文档:
Query#getScalarResult(): Retrieves a flat/rectangular result set of scalar values that can contain duplicate data. The pure/mixed distinction does not apply.
您的$result
变量可能不是对象。在其上运行var_dump
进行验证。这可能就是为什么你不能在上面调用->toArray()
的原因
多亏了stackoverflow开发人员,我找到了一个解决方案:请在这里查看!
事实上,我需要在json编码上使用第二个参数,如下所示来删除双引号:
$data = json_encode($array, JSON_NUMERIC_CHECK);
非常感谢@Dawid Sajdak的回答。问题是数组中有字符串,或者数据库中的值是十进制类型。