Skip to content

5.3 Query Builder count() Sql error - PR proposal #15232

@davidrushton

Description

@davidrushton

This might be related to #14997.

After upgrading to Laravel 5.3, I get the following Sql error.

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 (SQL: select count(*) as aggregate from `items` where `type` = product and `items`.`deleted_at` is null order by)

I think i've traced it down to the fact that in my repository, I call count() on an already created Builder instance which has an order by (to create a manual paginator for DataTables).

$query = $this->buildSearchQuery($type, $filters, $sort);
$total = $query->count();
...

The Query\Builder class has the following method:

public function aggregate($function, $columns = ['*'])
{
    $this->aggregate = compact('function', 'columns');

    $previousColumns = $this->columns;

    // We will also back up the select bindings since the select clause will be
    // removed when performing the aggregate function. Once the query is run
    // we will add the bindings back onto this query so they can get used.
    $previousSelectBindings = $this->bindings['select'];

    $this->bindings['select'] = [];
    $this->orders = [];

    $results = $this->get($columns);

    // Once we have executed the query, we will reset the aggregate property so
    // that more select queries can be executed against the database without
    // the aggregate value getting in the way when the grammar builds it.
    $this->aggregate = null;

    $this->columns = $previousColumns;

    $this->bindings['select'] = $previousSelectBindings;

    if (! $results->isEmpty()) {
        return array_change_key_case((array) $results[0])['aggregate'];
    }
}

I propose changing to the following, which works, using the existing paginator helper methods.

public function aggregate($function, $columns = ['*'])
{
    $this->aggregate = compact('function', 'columns');

    $this->backupFieldsForCount();

    $results = $this->get($columns);

    // Once we have executed the query, we will reset the aggregate property so
    // that more select queries can be executed against the database without
    // the aggregate value getting in the way when the grammar builds it.
    $this->aggregate = null;

    $this->restoreFieldsForCount();

    if (! $results->isEmpty()) {
        return array_change_key_case((array) $results[0])['aggregate'];
    }
}

Does anyone know if that will cause any problems with the count(), min(), max() or average() methods / queries?

David.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions