Skip to content

Commit 28df4c0

Browse files
author
David Nahodyl
committed
changed delete to work with primary key to better match native Laravel behavior, added deleteByRecordId for FM functionality
1 parent 68661de commit 28df4c0

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

src/Database/Query/FMBaseBuilder.php

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,82 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
176176
/**
177177
* Delete records from the database.
178178
*
179+
* @param null $recordId
179180
* @return int
180181
* @throws FileMakerDataApiException
181182
*/
182-
public function delete($recordId = null)
183+
public function delete($id = null): int
183184
{
184185
// If an ID is passed to the method we will delete the record with this internal FileMaker record ID
185-
if (!is_null($recordId)) {
186-
$this->recordId($recordId);
186+
if (! is_null($id)) {
187+
$this->where($this->defaultKeyName(), '=', $id);
187188
}
188-
189189
$this->applyBeforeQueryCallbacks();
190190

191+
// Check if we have a record ID to delete or if this is a query for a bulk delete
192+
if ($this->getRecordId() === null){
193+
// There's no individual record ID to delete, so do a bulk delete
194+
return $this->bulkDeleteFromQuery();
195+
}
196+
197+
198+
try {
199+
$this->connection->deleteRecord($this);
200+
} catch (FileMakerDataApiException $e) {
201+
if ($e->getCode() === 101) {
202+
// no record was found to be deleted, return modified count of 0
203+
return 0;
204+
} else {
205+
throw $e;
206+
}
207+
}
208+
// we deleted the record, return modified count of 1
209+
return 1;
210+
}
211+
212+
/**
213+
* Do a bulk delete from a where query
214+
*
215+
* @return int
216+
* @throws FileMakerDataApiException
217+
*/
218+
protected function bulkDeleteFromQuery(): int
219+
{
220+
221+
$records = $this->get();
222+
$deleteCount = 0;
223+
foreach ($records as $record) {
224+
try {
225+
$recordId = $record['recordId'];
226+
$this->deleteByRecordId($recordId);
227+
// increment our delete counter if we didn't hit an exception
228+
$deleteCount++;
229+
} catch (FileMakerDataApiException $e) {
230+
if ($e->getCode() === 101) {
231+
// no record was found to be deleted
232+
// continue on to the next record to attempt to delete without incrementing $deleteCount
233+
} else {
234+
throw $e;
235+
}
236+
}
237+
}
238+
// Return the count of deleted records
239+
return $deleteCount;
240+
241+
}
242+
243+
/**
244+
* Delete a record using the internal FileMaker record ID
245+
*
246+
* @param $recordId
247+
* @return int
248+
* @throws FileMakerDataApiException
249+
*/
250+
public function deleteByRecordId($recordId): int
251+
{
252+
253+
$this->recordId = $recordId;
254+
191255
try {
192256
$this->connection->deleteRecord($this);
193257
} catch (FileMakerDataApiException $e) {
@@ -200,6 +264,7 @@ public function delete($recordId = null)
200264
}
201265
// we deleted the record, return modified count of 1
202266
return 1;
267+
203268
}
204269

205270
/**

0 commit comments

Comments
 (0)