helpscout / helpscout-api-php

PHP Wrapper for the Help Scout API
MIT License
98 stars 62 forks source link

Issue when calling getPage on conversations. #313

Closed IronicTrout closed 12 months ago

IronicTrout commented 12 months ago

Thank you for taking the time to submit an issue with all the details shown below. Our engineering team monitors issues submitted and strives to respond with 1-2 business days.

Current behavior Gets the same page (first page only?)

Expected behavior Gets the correct page

$filters = (new \HelpScout\Api\Conversations\ConversationFilters())        
            ->inStatus('all')
            ->sortField('createdAt')
            ->sortOrder('asc')
            ->withQuery($query);

        // Get conversations with the filter
        $conversations = $helpscout_client->conversations()->list($filters);

        // If a specific page is being fetched, get it
        $total_pages = $conversations->getTotalPageCount();
        if (is_numeric($page) && $page >= 1 && $page <= $total_pages) {

            $conversations->getPage($page);
        }

        $conversations = $conversations->toArray();

I'm running the following code and it always seems to be fetching the same page of conversations and I'm not sure why. I know that $page is coming into the function correctly but it always seems to be fetching the same 25 results. Any help would be much appreciated!

miguelrs commented 12 months ago

Hey @IronicTrout!

I think your problem is you are calling $conversations->getPage($page);, but you're not assigning the result to the variable, so when you call $conversations->toArray(); at the end, $conversations still contains the first page.

You should do this instead:

$conversations = $conversations->getPage($page);

Like this, getPage($page) returns a new instance of PagedCollection containing the page you requested, and you replace the current value of the $conversations variable with that new instance.

IronicTrout commented 12 months ago

Ah what a bone-head mistake. Thank you for your help!