у меня есть этот контроллер с доктриной запросом:форматирования результата массива запрос от доктрины простого массива
$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;
Но у меня есть эта ошибка:
Error: Call to a member function toArray() on a non-object 500 Internal Server Error - 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 для Highcharts.
UPDATE
Если я иду на странице, чтобы посмотреть JSon результаты (см ниже 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"}]
индекс является имя столбца в моей базе данных и номера являются значениями для индекса в моих данных база.
Если я делаю 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]
Посмотрите мое обновление в моем первоначальном посте. Спасибо. –