1

I have this controller with a doctrine query:

$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;

But I have this error:

Error: Call to a member function toArray() on a non-object 500 Internal Server Error - FatalErrorException

But if I try this in my controller, it works: $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;

it returns me: [1,5,7,85,74,24,9,6,5,4,8555]

I need to render my query results into array only to pass the data values into json for Highcharts.


UPDATE

If I go on the page to watch json resultS (see the controlelr below), with my queries I have this result:

[{"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 is the name of the column in my database and the numbers are the values for index in my database.

If I make a var_dump, this is the results:

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=10)</i>
  0 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1700.000'</font> <i>(length=8)</i>
  1 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1200.000'</font> <i>(length=8)</i>
  2 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1200.000'</font> <i>(length=8)</i>
  3 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1304.000'</font> <i>(length=8)</i>
  4 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1800.000'</font> <i>(length=8)</i>
  5 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2012.000'</font> <i>(length=8)</i>
  6 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2048.000'</font> <i>(length=8)</i>
  7 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'1048.000'</font> <i>(length=8)</i>
  8 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'3000.000'</font> <i>(length=8)</i>
  9 <font color='#888a85'>=&gt;</font> 
    <b>array</b> <i>(size=1)</i>
      'index' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'5421.000'</font> <i>(length=8)</i>
</pre>

Like I said, the results are made in order to be in a graphic from highchart.js. This is the script in my view:

$(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);
    });
});

The highchart to have a simple array to display results in graphics, because when I use the following code for test in my controller, results are displaying correctly:

 $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;

it returns me:

/*var_dump*/
<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=11)</i>
  0 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>1</font>
  1 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>5</font>
  2 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>7</font>
  3 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>85</font>
  4 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>74</font>
  5 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>24</font>
  6 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>9</font>
  7 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>6</font>
  8 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>5</font>
  9 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>4</font>
  10 <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>8555</font>
</pre>

/*json response*/
[1,5,7,85,74,24,9,6,5,4,8555]

Instead of

[{"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"}]

I think I need to make the results form my database like this:

[1700.000, 1200.000,1200.000,1304.000,1800.000,2012.000,2048.000,1048.000,3000.000,5421.000]
french_dev
  • 2,117
  • 10
  • 44
  • 85

3 Answers3

1

Indeed, the problem is with data. In fact I see two problems: index means nothing to Highcharts. There should be y. Also your values are strings, while should be numbers.

Or, I think simple preprocessing will be enough:

$.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);
});
Paweł Fus
  • 44,795
  • 3
  • 61
  • 77
  • I don't understand what you suggested me. Indeed, I need to return an array with integer, not strings with double quotes in an array!. In fact, my values in database for index are decimals not integer. – french_dev May 04 '15 at 09:36
  • 1
    What I wanted to say is that you can change format in your backend from `[ {"index": "value"} ]` to `[ {"y": value } ]`. Or use above parser. – Paweł Fus May 04 '15 at 10:04
  • Fus, thank you for reply, of course I keep your suggestion for y values and highccharts properties. You show me another problem in my code that I need to fix. – french_dev May 04 '15 at 11:24
  • 1
    Yes, that's just a suggestion. If you prefer your format, use snippet above to get working with "index" format. – Paweł Fus May 04 '15 at 12:22
0

From the documentation:

Query#getScalarResult(): Retrieves a flat/rectangular result set of scalar values that can contain duplicate data. The pure/mixed distinction does not apply.

Your $result variable is probably not an object. Run a var_dump on it to verify. This is probably why you can't call ->toArray() on it.

Danny Kopping
  • 4,862
  • 2
  • 29
  • 38
0

I found a soltion thanks to stackoverflow developper: check here !

In fact I need to use a second parameter on my json encode like this to remove the double quote:

$data = json_encode($array, JSON_NUMERIC_CHECK);

A great thank to @Dawid Sajdak for his answer. The problem was that I have strings in array, or the values in my database are decimal type.

Community
  • 1
  • 1
french_dev
  • 2,117
  • 10
  • 44
  • 85