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
You can wrap your operations in a database transaction so that all calls may be rolled back in the event of an error. Using transactions has the added benefit of delaying any event handlers until all of your API calls are complete.
253
+
254
+
Note: The transaction service was introduced in Pace 29.0-1704.
255
+
256
+
### Using a closure
257
+
258
+
Use the `transaction()` method to execute your operations in a database transaction. Pace will rollback the transaction in the event of a server-side error, and the API client will automatically rollback the transaction if a PHP exception is thrown. If the closure executes successfully, and there are no server-side errors, the transaction will be committed.
259
+
260
+
```php
261
+
$pace->transaction(function () use ($pace) {
262
+
$job = $pace->model('Job');
263
+
$job->customer = 'HOUSE';
264
+
$job->description = 'Test Order';
265
+
$job->jobType = 10;
266
+
$job->adminStatus = 'O';
267
+
$job->save();
268
+
269
+
$jobPart = $job->jobParts()->first();
270
+
271
+
$jobMaterial = $pace->model('JobMaterial');
272
+
$jobMaterial->job = $jobPart->job;
273
+
$jobMaterial->jobPart = $jobPart->jobPart;
274
+
$jobMaterial->inventoryItem = 'ABC123';
275
+
$jobMaterial->plannedQuantity = 100;
276
+
$jobMaterial->save();
277
+
278
+
throw new Exception('Just kidding. Roll it back.');
279
+
});
280
+
```
281
+
282
+
### Using transactions manually
283
+
284
+
Alternatively, you may call the `startTransaction()`, `rollbackTransaction()` and `commitTransaction()` methods manually.
285
+
286
+
```php
287
+
$pace->startTransaction();
288
+
289
+
$csr = $pace->model('CSR');
290
+
$csr->name = 'Definitely Not Evil';
291
+
$csr->save();
292
+
293
+
if ($csr->id == 666) {
294
+
// Oh no. They are evil!
295
+
$pace->rollbackTransaction();
296
+
} else {
297
+
$pace->commitTransaction();
298
+
}
299
+
```
300
+
250
301
## JSON
251
302
252
303
Both the `Model` and `KeyCollection` classes implemement the `JsonSerializable` interface and casting either class to a string will generate JSON.
0 commit comments