You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -179,22 +180,29 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
179
180
/**
180
181
* Delete records from the database.
181
182
*
183
+
* @param null $recordId
182
184
* @return int
183
185
* @throws FileMakerDataApiException
184
186
*/
185
-
publicfunctiondelete($recordId = null)
187
+
publicfunctiondelete($id = null): int
186
188
{
187
189
// If an ID is passed to the method we will delete the record with this internal FileMaker record ID
188
-
if (! is_null($recordId)) {
189
-
$this->recordId($recordId);
190
+
if (! is_null($id)) {
191
+
$this->where($this->defaultKeyName(), '=', $id);
190
192
}
191
-
192
193
$this->applyBeforeQueryCallbacks();
193
194
195
+
// Check if we have a record ID to delete or if this is a query for a bulk delete
196
+
if ($this->getRecordId() === null){
197
+
// There's no individual record ID to delete, so do a bulk delete
198
+
return$this->bulkDeleteFromQuery();
199
+
}
200
+
201
+
194
202
try {
195
203
$this->connection->deleteRecord($this);
196
-
} catch (FileMakerDataApiException$e){
197
-
if ($e->getCode() === 101){
204
+
} catch (FileMakerDataApiException$e){
205
+
if ($e->getCode() === 101){
198
206
// no record was found to be deleted, return modified count of 0
199
207
return0;
200
208
} else {
@@ -205,6 +213,64 @@ public function delete($recordId = null)
205
213
return1;
206
214
}
207
215
216
+
/**
217
+
* Do a bulk delete from a where query
218
+
*
219
+
* @return int
220
+
* @throws FileMakerDataApiException
221
+
*/
222
+
protectedfunctionbulkDeleteFromQuery(): int
223
+
{
224
+
225
+
$records = $this->get();
226
+
$deleteCount = 0;
227
+
foreach ($recordsas$record) {
228
+
try {
229
+
$recordId = $record['recordId'];
230
+
$this->deleteByRecordId($recordId);
231
+
// increment our delete counter if we didn't hit an exception
232
+
$deleteCount++;
233
+
} catch (FileMakerDataApiException$e) {
234
+
if ($e->getCode() === 101) {
235
+
// no record was found to be deleted
236
+
// continue on to the next record to attempt to delete without incrementing $deleteCount
237
+
} else {
238
+
throw$e;
239
+
}
240
+
}
241
+
}
242
+
// Return the count of deleted records
243
+
return$deleteCount;
244
+
245
+
}
246
+
247
+
/**
248
+
* Delete a record using the internal FileMaker record ID
249
+
*
250
+
* @param $recordId
251
+
* @return int
252
+
* @throws FileMakerDataApiException
253
+
*/
254
+
publicfunctiondeleteByRecordId($recordId): int
255
+
{
256
+
257
+
$this->recordId = $recordId;
258
+
259
+
try {
260
+
$this->connection->deleteRecord($this);
261
+
} catch (FileMakerDataApiException$e) {
262
+
if ($e->getCode() === 101) {
263
+
// no record was found to be deleted, return modified count of 0
264
+
return0;
265
+
} else {
266
+
throw$e;
267
+
}
268
+
}
269
+
// we deleted the record, return modified count of 1
270
+
return1;
271
+
272
+
}
273
+
208
274
/**
209
275
* Returns the internal FileMaker record ID returned in a previous query, used for things like edits and deletes. This is not the primary key in your database.
210
276
* @return mixed
@@ -280,7 +346,7 @@ public function script($name, $param = null): FMBaseBuilder
280
346
$this->script = $name;
281
347
282
348
// set the script parameter if one was passed in
283
-
if ($param){
349
+
if ($param){
284
350
$this->scriptParam = $param;
285
351
}
286
352
@@ -298,7 +364,7 @@ public function scriptPresort($name, $param = null): FMBaseBuilder
298
364
$this->scriptPresort = $name;
299
365
300
366
// set the script parameter if one was passed in
301
-
if ($param){
367
+
if ($param){
302
368
$this->scriptPresortParam = $param;
303
369
}
304
370
@@ -316,7 +382,7 @@ public function scriptPrerequest($name, $param = null): FMBaseBuilder
316
382
$this->scriptPrerequest = $name;
317
383
318
384
// set the script parameter if one was passed in
319
-
if ($param){
385
+
if ($param){
320
386
$this->scriptPrerequestParam = $param;
321
387
}
322
388
@@ -356,7 +422,7 @@ public function get($columns = ['*'])
0 commit comments