How to Check for Entity Changes
-
The Session tracks all changes made to all entities that it has either loaded, stored, or queried for,
and persists to the server only what is needed whensaveChanges
is called. -
This article describes how to check for changes made in a specific entity within a session.
To check for changes on all tracked entities, see Check for session changes. -
In this page:
Check for entity changes
-
The session's advanced property
hasChanged
indicates whether the specified entity was added, modified, or deleted within the session. -
Note: The
hasChanged
property is cleared (reset tofalse
) after callingsaveChanges
.
$session = $store->openSession();
try {
// Store a new entity within the session
// =====================================
$employee = new Employee();
$employee->setFirstName("John");
$employee->setLastName("Doe");
$session->store($employee, "employees/1-A");
// '$hasChanged' will be TRUE
$hasChanged = $session->advanced()->hasChanged($employee);
// 'HasChanged' will reset to FALSE after saving changes
$session->saveChanges();
$hasChanged = $session->advanced()->hasChanged($employee);
// Load & modify entity within the session
// =======================================
$employee = $session->load(Employee::class, "employees/1-A");
$hasChanged = $session->advanced()->hasChanged($employee); // FALSE
$employee->setLastName("Brown");
$hasChanged = $session->advanced()->hasChanged($employee); // TRUE
$session->saveChanges();
$hasChanged = $session->advanced()->hasChanged($employee); // FALSE
} finally {
$session->close();
}
Get entity changes
-
Use the advanced session
whatChangedFor
method to get all changes made in the specified entity
within the session. -
Details will include:
- The name and path of the changed field
- Its old and new values
- The type of change
Example I
$session = $store->openSession();
try {
// Store (add) a new entity, it will be tracked by the session
$employee = new Employee();
$employee->setFirstName("John");
$employee->setLastName("Doe");
$session->store($employee, "employees/1-A");
// Get the changes for the entity in the session
// Call 'WhatChangedFor', pass the entity object in the param
$changesForEmployee = $session->advanced()->whatChangedFor($employee);
$this->assertCount($changesForEmployee, 1); // a single change for this entity (adding)
// Get the change type
$changeType = $changesForEmployee[0]->getChange();
$this->assertTrue($changeType->isDocumentAdded());
$session->saveChanges();
} finally {
$session->close();
}
Example II
$session = $store->openSession();
try {
// Load the entity, it will be tracked by the session
$employee = $session->load(Employee::class, "employees/1-A");
// Modify the entity
$employee->setFirstName("Jim");
$employee->LastName("Brown");
// Get the changes for the entity in the session
// Call 'WhatChangedFor', pass the entity object in the param
$changesForEmployee = $session->advanced()->whatChangedFor($employee);
$this->assertEquals("FirstName", $changesForEmployee[0]->getFieldName());// Field name
$this->assertEquals("Jim", $changesForEmployee[0]->getFieldNewValue()); // New value
$this->assertTrue($changesForEmployee[0]->getChange()->isFieldChanged()); // Change type
$this->assertEquals("LastName", $changesForEmployee[1]->getFieldName());
$this->assertEquals("Brown", $changesForEmployee[1]->getFieldNewValue());
$this->assertTrue($changesForEmployee[1]->getChange()->isFieldChanged());
$session->saveChanges();
} finally {
$session->close();
}
Syntax
public function hasChanged(?object $entity): bool;
// WhatChangedFor
public function whatChangedFor(object $entity): DocumentsChangesArray;
ReturnValue | |
---|---|
DocumentsChanges |
List of changes made in the entity (see ChangeType class below for available change types) |
class DocumentsChanges
{
private mixed $fieldOldValue = null; // Previous field value
private mixed $fieldNewValue = null; // Current field value
private ChangeType $change; // Type of change that occurred
private ?string $fieldName = null; // Name of field on which the change occurred
private ?string $fieldPath = null; // Path of field on which the change occurred
public function getFieldFullName(): ?string; // Path + Name of field on which the change occurred
// ... getters and setters
}
class ChangeType
{
public function isDocumentDeleted(): bool;
public function isDocumentAdded(): bool;
public function isFieldChanged(): bool;
public function isNewField(): bool;
public function isRemovedField(): bool;
public function isArrayValueChanged(): bool;
public function isArrayValueAdded(): bool;
public function isArrayValueRemoved(): bool;
}