Closed msullivanJIT closed 2 years ago
Hey @msullivanJIT, thanks for opening this issue!
Is your goal to only grab 20 records or is your goal to loop over all the records in 20 record chunks?
The loop
method is primarily here for an efficient way to loop over all records. It does this by using paginate
under the hood and passes every record received to the callback. By default the loop
method retrieves records in chunks of 500 (the Autotask limit); using the records
method changes the page size of those chunks.
For example:
/**
* For 1000 records, this will make fifty requests
* because it is retrieving the records in chunks of 20.
*/
$records = $client
->companies()
->query()
->where('id', 'exist')
->records(20)
->loop(function (CompanyEntity $company) {
echo $company->companyName . PHP_EOL;
});
/**
* For 1000 records, this will make two requests
* because it is retrieving the records in chunks of 500.
*/
$records = $client
->companies()
->query()
->where('id', 'exist')
->loop(function (CompanyEntity $company) {
echo $company->companyName . PHP_EOL;
});
Now, the records
method can also be used with the paginate
and get
methods. For example:
/**
* This will return a page of 25 records.
*/
$records = $client->companies()->query()->where('id', 'exist')->records(25)->paginate();
/**
* This will return 25 records without pagination.
*/
$records = $client->companies()->query()->where('id', 'exist')->records(25)->get();
Now, as far as I know, Autotask does not support setting the number of records for a count query, which is likely why the count
method is not working when you specify records
. It is intended to show the number of records that meet the filter criteria, so a call like the following would probably return an appropriate number.
$client->companies()->query()->where('id', 'exist')->count();
Does that all answer your question?
I was totally misunderstanding, but this makes perfect sense. Thanks for the response!
No problem!
I'm trying to use records() in combination with loops to limit records returned, however I'm not seeing the results I'm expecting.
For example:
$client->companies()->query()->records(20)->where('id', 'exist')->count();
returns 0
if I use records() in a query with a loop it returns all results
$query = $client->companies()->query()->records(20)->where('id', 'exist');
$query->loop(function (CompanyEntity $company) { echo $company->companyName . PHP_EOL; });
I guess I could use paginate() for this, but I'm trying to understand how records() and even loop() for that matter would be used instead of paginate.