french_dev
french_dev

Reputation: 2177

format an array result query from doctrine to a simple array

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]

Upvotes: 1

Views: 805

Answers (3)

french_dev
french_dev

Reputation: 2177

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.

Upvotes: 0

Paweł Fus
Paweł Fus

Reputation: 45079

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

Upvotes: 1

Danny Kopping
Danny Kopping

Reputation: 5190

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.

Upvotes: 0

Related Questions