IsraelOrtuno / pipedrive

Complete Pipedrive API client for PHP
MIT License
166 stars 58 forks source link

How to fetch all data by calling all() method once without the pagination limitation as 100 per call? #65

Closed 181 closed 5 years ago

181 commented 5 years ago

How to fetch all data by calling all() method once without the pagination limitation as 100 per call? Anyone call help? could be just a param to pass in, just not sure about if this repository supports that or not. Cheers.

updated: figured out a function for this, list here in case anyone else who needs it:

// possible $type values: 'Organizations', 'Users', 'Deals', etc.
protected function getAllWithoutPaginationLimit($type, $limit=100, $start=0) {
        $api = '\Pipedrive::' . $type;

        $response = $api()->all(['limit'=>$limit, 'start'=>$start]);

        if(!$response->isSuccess()) { 
            $msg = 'The pipedrive api call for ' . $type . ' was not successful';
            abort(404, $msg);  // this is a laravel helper, use others if you are not using laravel.
        }

       if(!empty($response->getData())) {
            $resources = $response->getData();
        } else {
            $msg = 'There is no records of ' . $type . ' on Pipedrive website yet';
            abort('404', $msg); 
        }

        if(!empty($response->getContent()->additional_data->pagination->more_items_in_collection) && $response->getContent()->additional_data->pagination->more_items_in_collection === true) {
            $nextStart=$response->getContent()->additional_data->pagination->next_start;
            $nextPaged = $this->getAllWithoutPaginationLimit($type, $limit=$limit, $nextStart);
            $resources = array_merge($resources, $nextPaged);
        }

        return $resources;
    }
IsraelOrtuno commented 5 years ago

Actually I think this is the way to go 👍