Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

query builder with 'group' causes error

public static function getByDeviceArray($devices)
{
    return self::find([
        'columns' => 'DATE_FORMAT(start_time,"%Y-%m-%d %H:%i") AS hour, COUNT(*) AS count, AVG(duration) as avg_duration',
        'group' => 'DATE_FORMAT(start_time,"%Y-%m-%d %H:%i")'
    ]);
}

If the method call fails

Phalcon\Mvc\Model\Exception: Scanning error before 'DATE_FORMAT(star...' when parsing: SELECT DATE_FORMAT(start_time,"%Y-%m-%d %H:%i") AS hour, COUNT(*) AS count, AVG(duration) as avg_duration FROM [Model\Session] GROUP BY [DATE_FORMAT(start_time,"%Y-%m-%d %H:%i")] (178)

I think it's because of a frame group in []. How to avoid it?



33.6k
Accepted
answer

Change it by:

public static function getByDeviceArray($devices)
{
    return self::find([
        'columns' => 'DATE_FORMAT(start_time,"%Y-%m-%d %H:%i") AS hour, COUNT(*) AS count, AVG(duration) as avg_duration',
        'group' => 'hour'
    ]);
}

thanks, it works!