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

WIP QueryBuilder pagination #11

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Conversation

joelambert
Copy link
Member

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.

@coveralls
Copy link

Coverage Status

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

6 similar comments
@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

@coveralls
Copy link

Coverage Status

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

{
global $paged;

if (isset($page)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was missing this feature and stumbled upon this merge request. I quickly checked how it was implemented by curiosity and I might have found an issue here : )

Since the $page attribute has a default value in the function params it will always be set. This means that if the user doesn't pass the $page attribute this will always reset the $paged value to 1.

One solution is to set the $page = NULL in the params and check if $page !== NULL here instead of the isset. I still didn't test it in a real wordpress setup so I might be wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I guess the if is redundant here. I can't see that it would cause any issue but you're right that it will always be set.

I think it's important to provide a default as I can't think of a scenario where when providing no $page param you wouldn't want the first page back.

@nealoke
Copy link

nealoke commented Nov 3, 2021

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

@martinpl
Copy link

martinpl commented Nov 3, 2021

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);
}

@joelambert
Copy link
Member Author

joelambert commented Nov 3, 2021

@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?

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

Successfully merging this pull request may close these issues.

5 participants