Skip to content

Commit 4321a35

Browse files
author
David Nahodyl
committed
Reorganization and changing behavior of edits to not error when editing a record which has been deleted to better match Laravel's expected behavior
1 parent 3517062 commit 4321a35

File tree

4 files changed

+114
-91
lines changed

4 files changed

+114
-91
lines changed

src/Database/Eloquent/FMEloquentBuilder.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function all()
6262
$this->query->limit(1000000000000000000);
6363
}
6464

65-
$records = $this->query->get($this);
65+
$records = $this->query->get();
6666
$models = $this->model->createModelsFromRecordSet($records);
6767
return $models;
6868
}
@@ -207,8 +207,6 @@ public function editRecord()
207207
$eachResponse = $this->query->recordId($model->getRecordId())->setContainer($containerField, $model->getAttribute($containerField));
208208
$this->model->setModId($this->getModIdFromFmResponse($eachResponse));
209209
}
210-
211-
return true;
212210
}
213211

214212
public function createRecord()
@@ -238,19 +236,6 @@ public function createRecord()
238236
$eachResponse = $this->query->recordId($model->getRecordId())->setContainer($containerField, $model->getAttribute($containerField));
239237
$this->model->setModId($this->getModIdFromFmResponse($eachResponse));
240238
}
241-
242-
return true;
243-
}
244-
245-
/**
246-
* Update records in the database.
247-
*
248-
* @param array $values
249-
* @return int
250-
*/
251-
public function update(array $values)
252-
{
253-
return $this->toBase()->update($values);
254239
}
255240

256241
protected function getModIdFromFmResponse($response)

src/Database/Eloquent/FMModel.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BlueFeather\EloquentFileMaker\Database\Eloquent\Concerns\FMHasRelationships;
66
use BlueFeather\EloquentFileMaker\Database\Query\FMBaseBuilder;
7+
use BlueFeather\EloquentFileMaker\Exceptions\FileMakerDataApiException;
78
use Illuminate\Database\Eloquent\Builder;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
@@ -291,7 +292,16 @@ protected function performUpdate(Builder $query)
291292
$dirty = $this->getDirty();
292293

293294
if (count($dirty) > 0) {
294-
$query->editRecord();
295+
try {
296+
$query->editRecord();
297+
} catch (FileMakerDataApiException $e ){
298+
// attempting to update and not actually modifying a record just returns a 0 by default to show no records were modified
299+
// If we don't actually modify anything it isn't considered an error in Laravel and we just continue
300+
if ($e->getCode() !== 101){
301+
// There was some error other than record missing, so throw it
302+
throw $e;
303+
}
304+
}
295305

296306
$this->syncChanges();
297307

@@ -305,8 +315,8 @@ protected function performUpdate(Builder $query)
305315
*
306316
* Set the keys for a save update query.
307317
*
308-
* @param \Illuminate\Database\Eloquent\Builder $query
309-
* @return \Illuminate\Database\Eloquent\Builder
318+
* @param Builder $query
319+
* @return Builder
310320
*/
311321
protected function setKeysForSaveQuery($query)
312322
{
@@ -318,7 +328,7 @@ protected function setKeysForSaveQuery($query)
318328
/**
319329
* Perform a model insert operation.
320330
*
321-
* @param \Illuminate\Database\Eloquent\Builder $query
331+
* @param Builder $query
322332
* @return bool
323333
*/
324334
protected function performInsert(Builder $query)

src/Database/Query/FMBaseBuilder.php

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use BlueFeather\EloquentFileMaker\Exceptions\FileMakerDataApiException;
88
use BlueFeather\EloquentFileMaker\Services\FileMakerConnection;
99
use Illuminate\Database\Query\Builder;
10-
use Illuminate\Database\Query\Grammars\Grammar;
11-
use Illuminate\Database\Query\Processors\Processor;
1210
use Illuminate\Http\File;
1311
use Illuminate\Support\Collection;
1412
use InvalidArgumentException;
@@ -156,21 +154,6 @@ class FMBaseBuilder extends Builder
156154
public $containerFile;
157155

158156

159-
/**
160-
* Create a new query builder instance.
161-
*
162-
* @param FileMakerConnection $connection
163-
* @param \Illuminate\Database\Query\Grammars\Grammar|null $grammar
164-
* @param \Illuminate\Database\Query\Processors\Processor|null $processor
165-
* @return void
166-
*/
167-
public function __construct(FileMakerConnection $connection,
168-
Grammar $grammar = null,
169-
Processor $processor = null)
170-
{
171-
$this->connection = $connection;
172-
}
173-
174157

175158
/**
176159
* Add a basic where clause to the query.
@@ -219,28 +202,33 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
219202
return $this;
220203
}
221204

222-
// convenience method for getting the first result of a find
223-
public function first($columns = ['*'])
224-
{
225-
// set the limit so we don't query more than we need to
226-
$result = $this->limit(1)->get()->first();
227-
return $result;
228-
}
229-
230205
/**
231206
* Delete records from the database.
232207
*
233208
* @return int
209+
* @throws FileMakerDataApiException
234210
*/
235211
public function delete($recordId = null)
236212
{
237-
$this->recordId($recordId ?? $this->getRecordId());
213+
// If an ID is passed to the method we will delete the record with this internal FileMaker record ID
214+
if (! is_null($recordId)) {
215+
$this->recordId($recordId);
216+
}
238217

239-
// return 0 if there's no record ID, as we can't delete anything
240-
// deleting nothing is supported normally, but should return a 0
241-
if ($this->getRecordId() === null) return 0;
218+
$this->applyBeforeQueryCallbacks();
242219

243-
return $this->connection->deleteRecord($this);
220+
try {
221+
$this->connection->deleteRecord($this);
222+
} catch (FileMakerDataApiException $e){
223+
if ($e->getCode() === 101){
224+
// no record was found to be deleted, return modified count of 0
225+
return 0;
226+
} else {
227+
throw $e;
228+
}
229+
}
230+
// we deleted the record, return modified count of 1
231+
return 1;
244232
}
245233

246234
/**
@@ -253,7 +241,8 @@ public function getRecordId()
253241
}
254242

255243
/**
256-
* @param mixed $recordId
244+
* Set the internal FileMaker Record ID to be used for these queries
245+
* @param int $recordId
257246
*/
258247
public function recordId($recordId)
259248
{
@@ -275,7 +264,8 @@ public function orderBy($column, $direction = self::ASCEND): FMBaseBuilder
275264
* @param string $direction
276265
* @return $this
277266
*/
278-
public function sort($column, $direction = self::ASCEND){
267+
public function sort($column, string $direction = self::ASCEND): FMBaseBuilder
268+
{
279269
return $this->orderBy($column, $direction);
280270
}
281271

@@ -289,7 +279,7 @@ public function orderByDesc($column): FMBaseBuilder
289279

290280
protected function appendSortOrder($column, $direction)
291281
{
292-
array_push($this->sorts, ['fieldName' => $this->getMappedFieldName($column), 'sortOrder' => $direction]);
282+
$this->sorts[] = ['fieldName' => $this->getMappedFieldName($column), 'sortOrder' => $direction];
293283
}
294284

295285
/**
@@ -305,9 +295,9 @@ public function limit($value): FMBaseBuilder
305295
}
306296

307297

308-
public function offset($offset): FMBaseBuilder
298+
public function offset($value): FMBaseBuilder
309299
{
310-
$this->offset = $offset;
300+
$this->offset = $value;
311301
return $this;
312302
}
313303

@@ -388,26 +378,22 @@ public function get($columns = ['*'])
388378
}
389379
}
390380
$records = collect($response['response']['data']);
381+
382+
// filter to only requested columns
383+
if ($columns !== ['*']){
384+
$records = $records->intersectByKeys(array_flip($columns));
385+
}
391386
return $records;
392387
}
393388

394-
/**
395-
* Get the database connection instance.
396-
*
397-
* @return FileMakerConnection
398-
*/
399-
public function getConnection()
400-
{
401-
return $this->connection;
402-
}
403389

404390
/**
405391
* Gets the gets the name of the mapped FileMaker field for a particular column
406392
*
407-
* @param $column
408-
* @return mixed
393+
* @param string $column
394+
* @return string
409395
*/
410-
protected function getMappedFieldName($column)
396+
protected function getMappedFieldName(string $column)
411397
{
412398
// remap the field name if the dev specified a mapping
413399
return array_flip($this->getFieldMapping())[$column] ?? $column;
@@ -419,7 +405,7 @@ protected function getMappedFieldName($column)
419405
* @param $array array An array of columns and their values
420406
* @return array
421407
*/
422-
protected function mapFieldNamesForArray($array)
408+
protected function mapFieldNamesForArray(array $array): array
423409
{
424410

425411
$mappedArray = [];
@@ -441,17 +427,18 @@ public function getFieldMapping(): array
441427
/**
442428
* @param array $fieldMapping
443429
*/
444-
public function setFieldMapping(array $fieldMapping): void
430+
public function setFieldMapping($fieldMapping): void
445431
{
446432
$this->fieldMapping = $fieldMapping;
447433
}
448434

449435
/**
450436
* Sets the current find request as an omit.
451437
* Optionally may pass false as a parameter to make a request NOT an omit if it was already set
452-
*
438+
* @param bool $boolean
439+
* @return FMBaseBuilder
453440
*/
454-
public function omit($boolean = true)
441+
public function omit($boolean = true): FMBaseBuilder
455442
{
456443

457444
$count = sizeof($this->wheres);
@@ -473,6 +460,7 @@ public function omit($boolean = true)
473460
* Retrieve the minimum value of a given column.
474461
*
475462
* @param string $column
463+
* @param string $direction
476464
* @return mixed
477465
*/
478466
public function min($column, $direction = self::ASCEND)
@@ -494,12 +482,23 @@ public function max($column)
494482
return $this->min($column, self::DESCEND);
495483
}
496484

485+
/**
486+
* Edit the record and get the raw FileMaker Data API Response
487+
*
488+
* @throws FileMakerDataApiException
489+
*/
497490
public function editRecord()
498491
{
499492
$response = $this->connection->editRecord($this);
500493
return $response;
501494
}
502495

496+
/**
497+
* Create a record and get the raw FileMaker Data API Response
498+
*
499+
* @return bool
500+
* @throws FileMakerDataApiException
501+
*/
503502
public function createRecord(){
504503
$response = $this->connection->createRecord($this);
505504
return $response;
@@ -580,7 +579,7 @@ public function update(array $values)
580579

581580
$this->fieldData($values);
582581

583-
return $this->connection->update($this, null);
582+
return $this->connection->update($this);
584583
}
585584

586585
public function layout($layoutName)

0 commit comments

Comments
 (0)