How to Defer Commands
-
The
defer
method allows you to register server commands via the session. -
All the deferred requests will be stored in the session and sent to the server in a single batch when
saveChanges
is called, along with any other changes/operations made on the session.
Thus, all deferred commands are executed as part of the session'ssaveChanges
transaction. -
In this page:
Defer commands example
// Defer is available in the session's `advanced` methods
$session->advanced()->defer(
// Define commands to be executed:
// i.e. Put a new document
new PutCommandDataWithJson(
"products/999-A",
null,
null,
[
"Name" => "My Product",
"Supplier" => "suppliers/1-A",
"@metadata" => [
"@collection" => "Products"
]
]
),
// Patch document
new PatchCommandData(
"products/999-A",
null,
PatchRequest::forScript("this.Supplier = 'suppliers/2-A';"),
null
),
// Force a revision to be created
new ForceRevisionCommandData("products/999-A"),
// Delete a document
new DeleteCommandData("products/1-A", null)
);
// All deferred commands will be sent to the server upon calling SaveChanges
$session->saveChanges();
Commands that can be deferred
The following commands implement the CommandDataInterface
interface and can be deferred:
- putCommandData
- deleteCommandData
- deletePrefixedCommandData
- patchCommandData
- batchPatchCommandData
- putAttachmentCommandData
- deleteAttachmentCommandData
- copyAttachmentCommandData
- moveAttachmentCommandData
- countersBatchCommandData
- putCompareExchangeCommandData
- deleteCompareExchangeCommandData
- copyTimeSeriesCommandData
- timeSeriesBatchCommandData
- forceRevisionCommandData
Syntax
/**
* Usage:
* - defer(CommandDataInterface $command): void
* - defer(CommandDataInterface ...$commands): void
* - defer(array<CommandDataInterface> $commands): void
*
* - defer(CommandDataInterface $command, array $commands): void
*
* Example:
* - defer($cmd);
* - defer($cmd1, $cmd2, $cmd3, $cmd4 ...)
* - defer([$cmd1, $cmd2, $cmd4, $cmd4, ...])
* - defer($cmd1, [$cmd2, $cmd3])
*
*/
public function defer(...$commands): void;
Parameter | Type | Description |
---|---|---|
$command | CommandDataInterface |
A command to be executed |
$commands | array<CommandDataInterface> |
An array of commands to be executed |
$commands | array |
An array of commands to be executed |