Session: Deleting Entities
Entities can be marked for deletion by using the delete
method, but will not be removed from the server until saveChanges
is called.
Syntax
public function delete(?object $entity): void;
public function delete(?string $id): void;
public function delete(?string $id, ?string $expectedChangeVector): void;
Parameters | ||
---|---|---|
entity | T |
instance of the entity to delete |
id | string |
ID of the entity to delete |
expectedChangeVector | string |
a change vector to use for concurrency checks |
Example I
$employee = $session->load(Employee::class, "employees/1");
$session->delete($employee);
$session->saveChanges();
Concurrency on Delete
If UseOptimisticConcurrency is set to 'true' (default 'false'), the Delete() method will use loaded 'employees/1' change vector for concurrency check and might throw ConcurrencyException.
Example II
$session->delete("employees/1");
$session->saveChanges();
Concurrency on Delete
In this overload, the Delete() method will not do any change vector based concurrency checks because the change vector for 'employees/1' is unknown.
Information
If entity is not tracked by session, then executing:
$session->delete("employees/1");
is equal to doing:
$session->advanced()->defer(new DeleteCommandData("employees/1", null));
Change Vector in DeleteCommandData
In this sample the change vector is null - this means that there will be no concurrency checks. A non-null and valid change vector value will trigger a concurrency check.
You can read more about defer operations here.