7
7
use BlueFeather \EloquentFileMaker \Exceptions \FileMakerDataApiException ;
8
8
use BlueFeather \EloquentFileMaker \Services \FileMakerConnection ;
9
9
use Illuminate \Database \Query \Builder ;
10
- use Illuminate \Database \Query \Grammars \Grammar ;
11
- use Illuminate \Database \Query \Processors \Processor ;
12
10
use Illuminate \Http \File ;
13
11
use Illuminate \Support \Collection ;
14
12
use InvalidArgumentException ;
@@ -156,21 +154,6 @@ class FMBaseBuilder extends Builder
156
154
public $ containerFile ;
157
155
158
156
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
-
174
157
175
158
/**
176
159
* Add a basic where clause to the query.
@@ -219,28 +202,33 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
219
202
return $ this ;
220
203
}
221
204
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
-
230
205
/**
231
206
* Delete records from the database.
232
207
*
233
208
* @return int
209
+ * @throws FileMakerDataApiException
234
210
*/
235
211
public function delete ($ recordId = null )
236
212
{
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
+ }
238
217
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 ();
242
219
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 ;
244
232
}
245
233
246
234
/**
@@ -253,7 +241,8 @@ public function getRecordId()
253
241
}
254
242
255
243
/**
256
- * @param mixed $recordId
244
+ * Set the internal FileMaker Record ID to be used for these queries
245
+ * @param int $recordId
257
246
*/
258
247
public function recordId ($ recordId )
259
248
{
@@ -275,7 +264,8 @@ public function orderBy($column, $direction = self::ASCEND): FMBaseBuilder
275
264
* @param string $direction
276
265
* @return $this
277
266
*/
278
- public function sort ($ column , $ direction = self ::ASCEND ){
267
+ public function sort ($ column , string $ direction = self ::ASCEND ): FMBaseBuilder
268
+ {
279
269
return $ this ->orderBy ($ column , $ direction );
280
270
}
281
271
@@ -289,7 +279,7 @@ public function orderByDesc($column): FMBaseBuilder
289
279
290
280
protected function appendSortOrder ($ column , $ direction )
291
281
{
292
- array_push ( $ this ->sorts , ['fieldName ' => $ this ->getMappedFieldName ($ column ), 'sortOrder ' => $ direction ]) ;
282
+ $ this ->sorts [] = ['fieldName ' => $ this ->getMappedFieldName ($ column ), 'sortOrder ' => $ direction ];
293
283
}
294
284
295
285
/**
@@ -305,9 +295,9 @@ public function limit($value): FMBaseBuilder
305
295
}
306
296
307
297
308
- public function offset ($ offset ): FMBaseBuilder
298
+ public function offset ($ value ): FMBaseBuilder
309
299
{
310
- $ this ->offset = $ offset ;
300
+ $ this ->offset = $ value ;
311
301
return $ this ;
312
302
}
313
303
@@ -388,26 +378,22 @@ public function get($columns = ['*'])
388
378
}
389
379
}
390
380
$ records = collect ($ response ['response ' ]['data ' ]);
381
+
382
+ // filter to only requested columns
383
+ if ($ columns !== ['* ' ]){
384
+ $ records = $ records ->intersectByKeys (array_flip ($ columns ));
385
+ }
391
386
return $ records ;
392
387
}
393
388
394
- /**
395
- * Get the database connection instance.
396
- *
397
- * @return FileMakerConnection
398
- */
399
- public function getConnection ()
400
- {
401
- return $ this ->connection ;
402
- }
403
389
404
390
/**
405
391
* Gets the gets the name of the mapped FileMaker field for a particular column
406
392
*
407
- * @param $column
408
- * @return mixed
393
+ * @param string $column
394
+ * @return string
409
395
*/
410
- protected function getMappedFieldName ($ column )
396
+ protected function getMappedFieldName (string $ column )
411
397
{
412
398
// remap the field name if the dev specified a mapping
413
399
return array_flip ($ this ->getFieldMapping ())[$ column ] ?? $ column ;
@@ -419,7 +405,7 @@ protected function getMappedFieldName($column)
419
405
* @param $array array An array of columns and their values
420
406
* @return array
421
407
*/
422
- protected function mapFieldNamesForArray ($ array )
408
+ protected function mapFieldNamesForArray (array $ array ): array
423
409
{
424
410
425
411
$ mappedArray = [];
@@ -441,17 +427,18 @@ public function getFieldMapping(): array
441
427
/**
442
428
* @param array $fieldMapping
443
429
*/
444
- public function setFieldMapping (array $ fieldMapping ): void
430
+ public function setFieldMapping ($ fieldMapping ): void
445
431
{
446
432
$ this ->fieldMapping = $ fieldMapping ;
447
433
}
448
434
449
435
/**
450
436
* Sets the current find request as an omit.
451
437
* 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
453
440
*/
454
- public function omit ($ boolean = true )
441
+ public function omit ($ boolean = true ): FMBaseBuilder
455
442
{
456
443
457
444
$ count = sizeof ($ this ->wheres );
@@ -473,6 +460,7 @@ public function omit($boolean = true)
473
460
* Retrieve the minimum value of a given column.
474
461
*
475
462
* @param string $column
463
+ * @param string $direction
476
464
* @return mixed
477
465
*/
478
466
public function min ($ column , $ direction = self ::ASCEND )
@@ -494,12 +482,23 @@ public function max($column)
494
482
return $ this ->min ($ column , self ::DESCEND );
495
483
}
496
484
485
+ /**
486
+ * Edit the record and get the raw FileMaker Data API Response
487
+ *
488
+ * @throws FileMakerDataApiException
489
+ */
497
490
public function editRecord ()
498
491
{
499
492
$ response = $ this ->connection ->editRecord ($ this );
500
493
return $ response ;
501
494
}
502
495
496
+ /**
497
+ * Create a record and get the raw FileMaker Data API Response
498
+ *
499
+ * @return bool
500
+ * @throws FileMakerDataApiException
501
+ */
503
502
public function createRecord (){
504
503
$ response = $ this ->connection ->createRecord ($ this );
505
504
return $ response ;
@@ -580,7 +579,7 @@ public function update(array $values)
580
579
581
580
$ this ->fieldData ($ values );
582
581
583
- return $ this ->connection ->update ($ this , null );
582
+ return $ this ->connection ->update ($ this );
584
583
}
585
584
586
585
public function layout ($ layoutName )
0 commit comments