Skip to content

Commit 4f6dfc9

Browse files
author
Michael Deck
committed
Add method to base builder to perform the find on the connection and handle the possible exceptions
- The added method will clean up the get and paginate methods and let them rely on the same functionality to prevent repetitive code. - Also, added the paginate method to the base builder to allow pagination of non-eloquent datasets.
1 parent 381f90e commit 4f6dfc9

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

src/Database/Eloquent/FMEloquentBuilder.php

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Contracts\Support\Arrayable;
1010
use Illuminate\Database\Eloquent\Builder;
1111
use Illuminate\Pagination\Paginator;
12+
use Illuminate\Support\Arr;
1213
use Illuminate\Support\Collection;
1314

1415
class FMEloquentBuilder extends Builder
@@ -194,7 +195,6 @@ public function editRecord()
194195
$this->model->setModId($this->getModIdFromFmResponse($response));
195196
}
196197

197-
198198
// also update any container fields which have changed
199199
// Only attempt to write modified container fields
200200
$modifiedContainerFields = $this->model->getContainersToWrite();
@@ -258,35 +258,12 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
258258

259259
$perPage = $perPage ?: $this->model->getPerPage();
260260

261-
/** @var FMBaseBuilder $query */
262-
$query = $this->toBase()->forPage($page, $perPage);
263-
264-
// prep items and total as null so we can handle 401 errors
265-
$total = null;
266-
$results = null;
267-
268-
// do the query and check for a 401. The query will 401 error if there are no rows which match the request
269-
try {
270-
$response = $this->getQuery()->getConnection()->performFind($query);
271-
} catch (FileMakerDataApiException $e) {
272-
if ($e->getCode() == 401) {
273-
$results = $this->model->newCollection();
274-
$total = 0;
275-
} else {
276-
throw $e;
277-
}
278-
}
279-
280-
// We didn't get a 401 and have received a real response, so parse it for the paginator
281-
if ($total === null && $results === null) {
282-
283-
$total = $response['response']['dataInfo']['foundCount'];
261+
$response = $this->forPage($page, $perPage)->getData();
284262

285-
$records = collect($response['response']['data']);
286-
287-
// start items as an empty array, but fill if the records
288-
$results = $this->model->createModelsFromRecordSet($records);
289-
}
263+
$total = Arr::get($response, 'response.dataInfo.foundCount', 0);
264+
$results = $this->model->createModelsFromRecordSet(
265+
collect(Arr::get($response, 'response.data'))
266+
);
290267

291268
return $this->paginator($results, $total, $perPage, $page, [
292269
'path' => Paginator::resolveCurrentPath(),

src/Database/Query/FMBaseBuilder.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Query\Builder;
1111
use Illuminate\Http\File;
1212
use Illuminate\Http\UploadedFile;
13+
use Illuminate\Pagination\Paginator;
1314
use Illuminate\Support\Arr;
1415
use Illuminate\Support\Collection;
1516
use InvalidArgumentException;
@@ -408,26 +409,44 @@ public function layoutResponse($name): FMBaseBuilder
408409
* @throws FileMakerDataApiException
409410
*/
410411
public function get($columns = ['*'])
412+
{
413+
$records = Arr::get($this->getData(), 'response.data', []);
414+
415+
// filter to only requested columns
416+
if ($columns !== ['*']) {
417+
$records = $records->intersectByKeys(array_flip($columns));
418+
}
419+
420+
return $records;
421+
}
422+
423+
protected function getData()
411424
{
412425
$this->computeWhereIns();
413426

414427
// Run the query and catch a 401 error if there are no records found - just return an empty collection
415428
try {
416-
$response = $this->connection->performFind($this);
429+
return $this->connection->performFind($this);
417430
} catch (FileMakerDataApiException $e) {
418-
if ($e->getCode() == 401) {
419-
return collect([]);
420-
} else {
421-
throw $e;
422-
}
423-
}
424-
$records = collect($response['response']['data']);
431+
throw_if($e->getCode() !== 401, $e);
425432

426-
// filter to only requested columns
427-
if ($columns !== ['*']) {
428-
$records = $records->intersectByKeys(array_flip($columns));
433+
return [];
429434
}
430-
return $records;
435+
}
436+
437+
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
438+
{
439+
$page = $page ?: Paginator::resolveCurrentPage($pageName);
440+
441+
$response = $this->forPage($page, $perPage)->getData();
442+
443+
$total = Arr::get($response, 'response.dataInfo.foundCount', 0);
444+
$results = collect(Arr::get($response, 'response.data'));
445+
446+
return $this->paginator($results, $total, $perPage, $page, [
447+
'path' => Paginator::resolveCurrentPath(),
448+
'pageName' => $pageName,
449+
]);
431450
}
432451

433452

0 commit comments

Comments
 (0)