Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculating total rows of a (limited) \Spot\Query #260

Open
Pnoexz opened this issue Feb 6, 2018 · 0 comments
Open

Calculating total rows of a (limited) \Spot\Query #260

Pnoexz opened this issue Feb 6, 2018 · 0 comments
Labels

Comments

@Pnoexz
Copy link

Pnoexz commented Feb 6, 2018

When getting paginated results, it's often common to also need the total rows for the query. I always hated doing this because I had to do the same query twice but only changing the select and limit parts of the query. Today, after reading the source code for both \Spot\Query and \Doctrine\DBAL\Query\QueryBuilder I found a much simpler way to do this. Following the lines of \Spot\Query::count(), I came up with:

   /**
     * Takes a Query object as a parater and removes the limit part of the
     * query, replaces the select with a simple COUNT(*), and removes the
     * order to slightly improve performance.
     *
     * @param Query $query
     *
     * @return int
     */
    protected function getTotalItemsFromQuery(Query $query): int
    {
        $countQuery = clone $query;
        $countQuery
            ->select('COUNT(*)')
            ->order([])
            ->limit(null, null);
        return $countQuery->count();
    }

Is it worth to look into adding this as a built-in feature or would it just be better to edit the documentation? Or is there an already way to do this that I've missed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants