Oihana PHP Arango

OnDeleteVertex

Trait OnDeleteVertex

This trait provides utilities to delete vertex when a document is removed and automatically purge related vertex documents when a vertex involved in an edge is deleted.

It is meant to be used within edge models with ArangoDB.

The purge behavior is controlled via the $purge property, which accepts values from the Purge enum:

  • Purge::OUTBOUND → Delete the "to" vertices when a "from" vertex is removed.
  • Purge::INBOUND → Delete the "from" vertices when a "to" vertex is removed.
  • Purge::BOTH → Delete both sides when the corresponding vertex is removed.

Example:

$edge->purge = Purge::BOTH;
$payload = new Payload(data: $vertexToDelete, target: $edge->from);
$edge->onDeleteVertex($payload);
Tags
author

Marc

version
1.0.0

Table of Contents

Constants

ON_DELETE_VERTEX  : string = 'onDeleteVertex'
Constant for the method name to hook deletion events.

Methods

onDeleteVertex()  : void
Invoked when a vertex document is deleted.
purgeVertices()  : void
Purge related vertex documents based on the defined purge direction.

Constants

ON_DELETE_VERTEX

Constant for the method name to hook deletion events.

public string ON_DELETE_VERTEX = 'onDeleteVertex'

Methods

onDeleteVertex()

Invoked when a vertex document is deleted.

public onDeleteVertex(Payload $payload) : void

This method :

  • Deletes all edges of the specific document resource ;
  • Determinates which related vertex documents to purge based on the $purge property ;
Parameters
$payload : Payload

The payload containing:

  • data : The deleted vertex or array of vertices.
  • target: The vertex model (from or to) from which deletion originates.
Tags
throws
Throwable

Example:

$payload = new Payload(data: $deletedVertex, target: $edge->from);
$edge->onDeleteVertex($payload);

purgeVertices()

Purge related vertex documents based on the defined purge direction.

private purgeVertices(array<string|int, mixed> $edges, object $target) : void

This method is automatically called by onDeleteVertex() after an edge's vertex has been deleted.

Usage

For example, suppose you have a WebAPI document connected to several Permission documents via edges. If you delete the WebAPI document and the purge mode is set to Purge::OUTBOUND or Purge::BOTH, all connected Permission vertices in their collection will be automatically removed.

Similarly, if a Permission vertex is deleted and the purge mode is Purge::INBOUND or Purge::BOTH, the related WebAPI vertices will be deleted according to the purge direction.

Graph illustration of purge directions

OUTBOUND (delete TO when FROM is deleted): [FROM: WebAPI] ---> [TO: Permission] DELETE WebAPI -> automatically DELETE Permission(s)

INBOUND (delete FROM when TO is deleted): [FROM: WebAPI] ---> [TO: Permission] DELETE Permission -> automatically DELETE WebAPI

BOTH (delete both sides): [FROM: WebAPI] ---> [TO: Permission] DELETE WebAPI -> DELETE Permission DELETE Permission -> DELETE WebAPI

Parameters
$edges : array<string|int, mixed>

Array of edge documents returned by deleteEdges().

$target : object

The vertex model that triggered the deletion.

Return values
void

Example:

$edges = $edge->deleteEdges(vertex: $vertexKeys, init: [AQL::CONTEXT => $edge->from]);
$edge->purgeVertices($edges, $edge->from);
On this page

Search results