Session: Loading Entities



load

The most basic way to load a single entity is to use the load method.

public function load(?string $className, ?string $id): ?object;
Parameter Type Description
id string An ID to load a single entity by
className string What entity type to load
Return Type Description
?object The loaded entity, or null if an entity with the given ID doesn't exist

Example

/** @var Employee $employee */
$employee = $session->load(Employee::class, "employees/1");

Starting with RavenDB version 4.x, only string identifiers are supported.
If you are upgrading from 3.x, this is a major change since in 3.x` non-string identifiers were supported.

load with include

When there is a 'relationship' between documents, those documents can be loaded in a single request call using the include + load methods. Learn more in How To Handle Document Relationships.

function include(?string $path): LoaderWithIncludeInterface;

public function load(string $className, array $ids): ObjectArray;
public function load(string $className, StringArray $ids): ObjectArray;

public function load(string $className, string $id): ?object;
Parameter Type Description
path string A path that the server should search the referenced documents by
className string What entity type to load
id string An ID to load a single entity by
ids array/StringArray An array of IDs to load entities by
Return Type Description
?object The loaded entity, or null if an entity with the given ID doesn't exist
ObjectArray An array of loaded entities
LoaderWithIncludeInterface The include method doesn't satidfy requests directly but returns an interface that can be used

Example I

We can use this code to also load an employee which made the order.

// loading 'products/1'
// including document found in 'supplier' property

/** @var Product $product */
$product = $session
    ->include("Supplier")
    ->load(Product::class, "products/1");

$supplier = $session->load(Supplier::class, $product->getSupplier()); // this will not make server call

Example II

// loading 'products/1'
// including document found in 'supplier' property
/** @var Product $product */
$product = $session
    ->include("Supplier")
    ->load(Product::class, "products/1");

$supplier = $session->load(Supplier::class, $product->getSupplier()); // this will not make server call

load - multiple entities

To load multiple entities at once, use one of the following load overloads.

public function load(string $className, array $ids): ObjectArray;
public function load(string $className, StringArray $ids): ObjectArray;
Parameter Type Description
className string What entity type to load
ids array/StringArray An array of IDs to load entities by
Return Type Description
ObjectArray An array of loaded entities

$employees = $session->load(Employee::class, "employees/1", "employees/2", "employees/3");

loadStartingWith

To load multiple entities that contain a common prefix, use the loadStartingWith method from the advanced session operations.

public function loadStartingWith(
    string $className,
    ?string $idPrefix,
    ?string $matches = null,
    int $start = 0,
    int $pageSize = 25,
    ?string $exclude = null,
    ?string $startAfter = null
): ObjectArray;
Parameter Type Description
className string What entity type to load
idPrefix string ID prefix: documents will be retrieved if their ID starts with the given prefix
matches string pipe (|) separated values, that document IDs (after 'idPrefix') should match.
? - any single character
* - any string of characters
start int number of documents to skip
pageSize int maximum number of documents to retrieve
exclude string pipe (|) separated values, that document IDs (after 'idPrefix') should not match.
? - any single character
* - any string of characters
startAfter string skip fetching document until the given ID is found, and return documents after this ID (default: null)
Return Type Description
ObjectArray an array of entities matching the given parameters

Example I

// return up to 128 entities with Id that starts with 'employees'
$result = $session
    ->advanced()
    ->loadStartingWith(Employee::class, "employees/", null, 0, 128);

Example II

// return up to 128 entities with Id that starts with 'employees/'
// and rest of the key begins with "1" or "2" e.g. employees/10, employees/25
$result = $session
    ->advanced()
    ->loadStartingWith(Employee::class, "employees/", "1*|2*", 0, 128);

conditionalLoad

This method can be used to check whether a document has been modified since the last time its change vector was recorded, so that the cost of loading it can be saved if it has not been modified.

The conditionalLoad method takes a document's change vector. If the entity is tracked by the session, this method returns the entity. If the entity is not tracked, it checks if the provided change vector matches the document's current change vector on the server side. If they match, the entity is not loaded. If the change vectors do not match, the document is loaded.

The method is accessible from the session advanced operations.

function conditionalLoad(?string $className, ?string $id, ?string $changeVector): ConditionalLoadResult;
Parameter Type Description
className string What entity type to load
id string The identifier of a document to load
changeVector string The change vector you want to compare with the server-side change vector. If the change vectors match, the document is not loaded.
Return Type Description
ConditionalLoadResult If the given change vector and the server side change vector do not match, the method returns the requested entity and its current change vector.
If the change vectors match, the method returns default as the entity, and the current change vector.
If the specified document, the method returns only default without a change vector.

Example

$session = $store->openSession();
try {
    $changeVector = "";
    $user = new User("Bob");

    $session->store($user, "users/1");
    $session->saveChanges();

    $changeVector = $session->advanced()->getChangeVectorFor($user);
} finally {
    $session->close();
}

$user = new User("Bob");
$changeVector = "a";

$session = $store->openSession();
try {
    // New session which does not track our User entity

    // The given change vector matches
    // the server-side change vector
    // Does not load the document
    $result1 = $session->advanced()
        ->conditionalLoad(User::class, "users/1", $changeVector);

    // Modify the document
    $user->setName("Bob Smith");
    $session->store($user);
    $session->saveChanges();

    // Change vectors do not match
    // Loads the document
    $result2 = $session->advanced()
        ->conditionalLoad(User::class, "users/1", $changeVector);
} finally {
    $session->close();
}

isLoaded

Use the isLoaded method from the advanced session operations To check if an entity is attached to a session (e.g. because it's been previously loaded).

isLoaded checks if an attempt to load a document has been already made during the current session, and returns true even if such an attemp was made and failed.
If, for example, the load method was used to load employees/3 during this session and failed because the document has been previously deleted, isLoaded will still return true for employees/3 for the remainder of the session just because of the attempt to load it.

function isLoaded(string $id): bool;
Parameter Type Description
id string The ID of the entity to perform the check for
Return Type Description
bool Indicates whether an entity with a given ID is loaded

Example

$isLoaded = $session->advanced()->isLoaded("employees/1"); //false
$employee = $session->load(Employee::class, "employees/1");
$isLoaded = $session->advanced()->isLoaded("employees/1"); // true