我有一个带有教义查询的控制器:
$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() 内部服务器错误 - FatalErrorException
但是如果我在我的控制器中尝试这个,它可以工作: $ 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;
它返回给我:[1,5,7,85,74,24,9,6,5,4,8555]
我需要将查询结果渲染到数组中,才能将数据值传递给json for Highcharts。
更新
如果我继续浏览页面以查看json结果(请参阅下面的控件),我的查询结果如下:
[{"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);
});
});
使用简单数组显示图形结果的高图,因为当我在控制器中使用以下代码进行测试时,结果显示正确:
$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]
答案 0 :(得分:1)
事实上,问题在于数据。事实上,我看到两个问题: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);
});
答案 1 :(得分:0)
来自文档:
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()
的原因。
答案 2 :(得分:0)
感谢stackoverflow developper,我找到了一个解决方案:check here!
实际上我需要在我的json编码上使用第二个参数来删除双引号:
$data = json_encode($array, JSON_NUMERIC_CHECK);
非常感谢@Dawid Sajdak的回答。问题是我在数组中有字符串,或者我的数据库中的值是十进制类型。