Switch Operations to a Different Database



Common operation: forDatabase

  • For reference, all common operations are listed here.

// Define default database on the store
$documentStore = new DocumentStore(
    ["yourServerURL"],
    "DefaultDB"
);
$documentStore->initialize();

try {
    // Use 'ForDatabase', get operation executor for another database
    /** @var OperationExecutor $opExecutor */
    $opExecutor = $documentStore->operations()->forDatabase("AnotherDB");

    // Send the operation, e.g. 'GetRevisionsOperation' will be executed on "AnotherDB"
    $revisionsInAnotherDB = $opExecutor->send(new GetRevisionsOperation(Order::class, "Orders/1-A"));

    // Without 'ForDatabase', the operation is executed "DefaultDB"
    $revisionsInDefaultDB = $documentStore->operations()->send(new GetRevisionsOperation(Company::class, "Company/1-A"));
} finally {
    $documentStore->close();
}

Syntax:

public function forDatabase(?string $databaseName): OperationExecutor;
Parameters Type Description
$databaseName ?string Name of the database to operate on
Return Value Description
OperationExecutor New instance of Operation Executor that is scoped to the requested database

Maintenance operation: maintenance.for_database

  • For reference, all maintenance operations are listed here.

// Define default database on the store
$documentStore = new DocumentStore(
    [ "yourServerURL" ],
    "DefaultDB"
);
$documentStore->initialize();

try {
    // Use 'ForDatabase', get maintenance operation executor for another database
    /** @var MaintenanceOperationExecutor $opExecutor */
    $opExecutor = $documentStore->maintenance()->forDatabase("AnotherDB");

    // Send the maintenance operation, e.g. get database stats for "AnotherDB"
    $statsForAnotherDB = $opExecutor->send(new GetStatisticsOperation());

    // Without 'ForDatabase', the stats are retrieved for "DefaultDB"
    $statsForDefaultDB = $documentStore->maintenance()->send(new GetStatisticsOperation());
} finally {
    $documentStore->close();
}

Syntax:

public function forDatabase(?string $databaseName): MaintenanceOperationExecutor;
Parameters Type Description
$databaseName ?string Name of the database to operate on
Return Value Description
MaintenanceOperationExecutor New instance of Maintenance Operation Executor that is scoped to the requested database