[英]format an array result query from doctrine to a simple array
我有一個帶有原理查詢的控制器:
$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]
我只需要將查詢結果呈現為數組,即可將數據值傳遞給Highcharts的json。
UPDATE
如果我在頁面上觀看json resultS(請參閱下面的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是數據庫中列的名稱, 數字是數據庫中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]
確實,問題出在數據上。 實際上,我看到兩個問題: 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的回答。 問題是我在數組中有字符串,或者數據庫中的值是十進制類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.