Average count in column which has same id

I am trying to display average of column which has same user_id:

    $average = Rating::average(
            'column'     => 'rating',
            'group' => 'user_id',
    $this->view-> averages = $average;


      {% for average in averages %}
        <td>{{ averages.rating }}</td>
       {% endfor %}

But it returns all the values, not average of values with same user_id.


group by user_id ?

edited Jan '18

Tested your exact code and it is working fine for me?

    [0] => Array
            [product_id] => 1
            [average] => 4.5000

    [1] => Array
            [product_id] => 2
            [average] => 3.8571


UPDATE! Just noticed a typo in your code:

This <td>{{ averages.rating }}</td> should be <td>{{ average.rating }}</td> your item is average, not averageS.


oh lord, my comment is totaly useless, you already grouped by user id, was too early in the morning so i didnt see it :D

edited Jan '18

I always get this:

SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for integer: "user_id" Undefined property: Phalcon\Mvc\Model\Row::$rating

It looks like I can't reach my main model of table. Without average function table are displayed correctly. Maybe this function is problem?


maybe model meta data cache problem ?



try with:

<td>{{ average.average }}</td>


<td>{{ average.user_id }}</td><td>{{ average.average }}</td>

should work to !