lumberjack-core icon indicating copy to clipboard operation
lumberjack-core copied to clipboard

WIP QueryBuilder pagination

Open joelambert opened this issue 4 years ago • 10 comments

This adds a paginate() method to the QueryBuilder that returns an instance of Timber's PostQuery instead of a Collection.

This needs to be tested in a WordPress install.

Possible enhancements could be an abstraction of the PostQuery class that gives access to the Pagination object as well as a Collection of posts rather than an Array.

joelambert avatar Aug 25 '20 11:08 joelambert

Coverage Status

Coverage decreased (-0.7%) to 88.747% when pulling 7c78e56def55fdbbe3d4c9f6a65549f4a7c8b89b on querybuilder-pagination into 5b71360ee0fc1bb7f4a78ec0a2aca41a4c2c30fb on master.

coveralls avatar Aug 25 '20 12:08 coveralls

Coverage Status

Coverage decreased (-0.7%) to 88.747% when pulling 7c78e56def55fdbbe3d4c9f6a65549f4a7c8b89b on querybuilder-pagination into 5b71360ee0fc1bb7f4a78ec0a2aca41a4c2c30fb on master.

coveralls avatar Aug 25 '20 12:08 coveralls

Coverage Status

Coverage decreased (-0.7%) to 88.747% when pulling 7c78e56def55fdbbe3d4c9f6a65549f4a7c8b89b on querybuilder-pagination into 5b71360ee0fc1bb7f4a78ec0a2aca41a4c2c30fb on master.

coveralls avatar Aug 25 '20 12:08 coveralls

Coverage Status

Coverage decreased (-0.7%) to 88.747% when pulling 7c78e56def55fdbbe3d4c9f6a65549f4a7c8b89b on querybuilder-pagination into 5b71360ee0fc1bb7f4a78ec0a2aca41a4c2c30fb on master.

coveralls avatar Aug 25 '20 12:08 coveralls

Coverage Status

Coverage decreased (-0.7%) to 88.747% when pulling 7c78e56def55fdbbe3d4c9f6a65549f4a7c8b89b on querybuilder-pagination into 5b71360ee0fc1bb7f4a78ec0a2aca41a4c2c30fb on master.

coveralls avatar Aug 25 '20 12:08 coveralls

Coverage Status

Coverage decreased (-0.7%) to 88.747% when pulling 7c78e56def55fdbbe3d4c9f6a65549f4a7c8b89b on querybuilder-pagination into 5b71360ee0fc1bb7f4a78ec0a2aca41a4c2c30fb on master.

coveralls avatar Aug 25 '20 12:08 coveralls

Coverage Status

Coverage decreased (-0.7%) to 88.747% when pulling 7c78e56def55fdbbe3d4c9f6a65549f4a7c8b89b on querybuilder-pagination into 5b71360ee0fc1bb7f4a78ec0a2aca41a4c2c30fb on master.

coveralls avatar Aug 25 '20 12:08 coveralls

Lumberjack is awesome but really needs this functionality. @joelambert is there a way we can help to get this merged? Pagination is quite helpful 😅.

nealoke avatar Nov 03 '21 09:11 nealoke

IMO paginate() should only set paged parameter and if empty set it from global $paged. So there would be needed two additional methods getTotalCount() for return found_posts and run() for running query. Advantage is that we still return collection and don't duplicate limit functionality and get functionality.

Code example:

public function paginate($page = null)
{
    if ($page) {
        $this->params['paged'] = $page;	
    } else {
        global $paged;
        $this->params['paged'] = $paged;
    }

    return $this;
}

public function getTotalCount() : int
{
    if (!$this->query) {
        $this->run();
    }

    return $this->query->found_posts;
}

public function run()
{
    $this->query = new PostQuery($this->getParameters(), $this->postClass);

    return $this;
}

public function get() : Collection
{
    if (!$this->query) {
        $this->run();
    }

    return collect($this->query);
}

martinpl avatar Nov 03 '21 10:11 martinpl

@MartinPL I'm not too keen on adding more internal state to the query builder. At the moment get() isn't a chainable method call and triggers the query, the PR as proposed makes paginate() behave the same. By adding internal state we get into temporal coupling issues and would need to work out when to refresh the internal state (e.g. subsequent calls to query builder methods that invalidate the query).

@adamtomat do you have any thoughts on this? To my mind, this PR is pretty close, the only question is whether we should write an abstraction around the PostQuery object that returns a collection? Maybe this is something we can add in a future release though?

joelambert avatar Nov 03 '21 12:11 joelambert