Oihana PHP Arango

EdgesExistTrait uses trait:short

Provides edge existence checking utilities for ArangoDB edge collections.

This trait extends EdgesCountTrait to efficiently determine whether edges exist between vertices using AQL queries.

Features:

  • Check if an edge exists between specific _from and _to vertices.
  • Check if an edge exists from a specific _from vertex.
  • Check if an edge exists to a specific _to vertex.
  • Supports bind variables and query customization via the $init array.

Usage

class Edges extends Documents
{
   use EdgesExistTrait;
}

$edges = new Edges($container, ['collection' => 'user_follows']);

// Check if an edge exists between two vertices
$exists = $edges->existEdge('1', '5');

// Check if an edge exists from a specific vertex
$existsFrom = $edges->existEdgeFrom('1');

// Check if an edge exists to a specific vertex
$existsTo = $edges->existEdgeTo('5');

Notes

  • Relies on countEdge() from EdgesCountTrait internally.
  • Returns true if at least one matching edge exists, false otherwise.
  • Throws exceptions if the query cannot be executed or if bind variables fail.
Tags
author

Marc Alcaraz (eKameleon)

version
1.0.0

Table of Contents

Methods

countAnyVertices()  : int
Counts all unique vertices connected in any direction from the given vertex.
countEdges()  : int
Counts the number of edge documents matching the specified vertices.
countInboundVertices()  : int
Counts all unique inbound vertices connected to the given 'to' vertex.
countOutboundVertices()  : int
Counts all unique outbound vertices connected from the given 'from' vertex.
countVertices()  : int
Counts all vertices connected with a specific direction.
existEdge()  : bool
Checks if an edge exists between the given '_from' and '_to' vertices.
existEdgeFrom()  : bool
Checks if at least one edge exists from the specified 'from' vertex.
existEdgeTo()  : bool
Checks if at least one edge exists to the specified 'to' vertex.
hasAnyVertex()  : bool
Checks if a specific target vertex is a neighbor in any direction.
hasInboundVertex()  : bool
Checks if a specific target vertex is an inbound neighbor of the start vertex.
hasOutboundVertex()  : bool
Checks if a specific target vertex is an outbound neighbor of the start vertex.
countEdgesQuery()  : string
Generates the count query and fill the binds array reference.
hasVertex()  : bool
Private helper to check for vertex existence in a specific direction.

Methods

countAnyVertices()

Counts all unique vertices connected in any direction from the given vertex.

public countAnyVertices([string|null $vertex = null ][, array<string|int, mixed> $init = [] ]) : int

This is a convenience method for countVertices(Traversal::ANY, ...). It counts unique neighbors (vertices), not relations (edges).

Parameters
$vertex : string|null = null

Optional '_key' or '_id' of the vertex to start from.

$init : array<string|int, mixed> = []

Optional query initialization array (e.g., AQL::GRAPH).

Tags
throws
ArangoException
BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
Return values
int

The total count of unique connected vertices.

countEdges()

Counts the number of edge documents matching the specified vertices.

public countEdges([string|null $from = null ][, string|null $to = null ][, array<string|int, mixed> $init = [] ]) : int

This counts the relations (edges), not the unique neighbors.

Parameters
$from : string|null = null

Optional '_from' vertex identifier.

$to : string|null = null

Optional '_to' vertex identifier.

$init : array<string|int, mixed> = []

Optional query options (e.g., AQL::BINDS, operator => Logic::OR).

Tags
throws
ArangoException
BindException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
Return values
int

The total count of matching edge documents.

countInboundVertices()

Counts all unique inbound vertices connected to the given 'to' vertex.

public countInboundVertices([string|null $to = null ][, array<string|int, mixed> $init = [] ]) : int

This is a convenience method for countVertices(Traversal::INBOUND, ...). It counts unique neighbors (vertices), not relations (edges).

Parameters
$to : string|null = null

Optional '_key' or '_id' of the vertex to traverse to.

$init : array<string|int, mixed> = []

Optional query initialization array (e.g., AQL::GRAPH).

Tags
throws
ArangoException
BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
Return values
int

The total count of unique inbound vertices.

countOutboundVertices()

Counts all unique outbound vertices connected from the given 'from' vertex.

public countOutboundVertices([string|null $from = null ][, array<string|int, mixed> $init = [] ]) : int

This is a convenience method for countVertices(Traversal::OUTBOUND, ...). It counts unique neighbors (vertices), not relations (edges).

Parameters
$from : string|null = null

Optional '_key' or '_id' of the vertex to start from.

$init : array<string|int, mixed> = []

Optional query initialization array (e.g., AQL::GRAPH).

Tags
throws
ArangoException
BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
Return values
int

The total count of unique outbound vertices.

countVertices()

Counts all vertices connected with a specific direction.

public countVertices(string $direction[, string|null $vertex = null ][, array{vertexRef?: string, edgeRef?: string|null, pathRef?: string|null, direction?: string, startVertex?: string, graph?: string|null, edgeCollection?: array|string|null, minDepth?: int|null, maxDepth?: int|null, prune?: string|array|null, options?: array|object|string|null, filter?: string|array|null, binds?: array, from?: string|null, to?: string|null, anyRef?: string|null, docRef?: string|null} $init = [] ]) : int

This counts the destination vertex documents.

Parameters
$direction : string

The direction of the relation: Traversal::OUTBOUND, Traversal::INBOUND, or Traversal::ANY.

$vertex : string|null = null

Optional '_key' or '_id' of the vertex to start the relation from.

$init : array{vertexRef?: string, edgeRef?: string|null, pathRef?: string|null, direction?: string, startVertex?: string, graph?: string|null, edgeCollection?: array|string|null, minDepth?: int|null, maxDepth?: int|null, prune?: string|array|null, options?: array|object|string|null, filter?: string|array|null, binds?: array, from?: string|null, to?: string|null, anyRef?: string|null, docRef?: string|null} = []

Optional query initialization and configuration array.

Tags
throws
ArangoException
BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
Return values
int

The total count of matching vertices.

existEdge()

Checks if an edge exists between the given '_from' and '_to' vertices.

public existEdge([string|null $from = null ][, string|null $to = null ][, array<string|int, mixed> $init = [] ]) : bool
Parameters
$from : string|null = null

Optional 'from' vertex unique key identifier.

$to : string|null = null

Optional 'to' vertex unique key identifier.

$init : array<string|int, mixed> = []

Optional query options, e.g., bind variables.

Tags
throws
ArangoException
BindException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
example
$edges = new Edges($container, ['collection' => 'user_follows']);
$exists = $edges->existEdge('1', '5');
if ($exists) {
    echo "Edge exists between users/1 and posts/5";
}
Return values
bool

True if at least one edge exists, false otherwise.

existEdgeFrom()

Checks if at least one edge exists from the specified 'from' vertex.

public existEdgeFrom(string $from[, array<string|int, mixed> $init = [] ]) : bool
Parameters
$from : string

The 'from' vertex unique key identifier.

$init : array<string|int, mixed> = []

Optional query options.

Tags
throws
ArangoException
BindException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
example
$edges = new Edges($container, ['collection' => 'user_follows']);
$existsFrom = $edges->existEdgeFrom('1');
if ($existsFrom)
{
    echo "There are edges from users/1";
}
Return values
bool

True if at least one edge exists, false otherwise.

existEdgeTo()

Checks if at least one edge exists to the specified 'to' vertex.

public existEdgeTo(string $to[, array<string|int, mixed> $init = [] ]) : bool
Parameters
$to : string

The 'to' vertex unique key identifier.

$init : array<string|int, mixed> = []

Optional query options.

Tags
throws
ArangoException
BindException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
example
$edges = new Edges($container, ['collection' => 'user_follows']);
$existsTo = $edges->existEdgeTo('5');
if ($existsTo)
{
    echo "There are edges to posts/5";
}
Return values
bool

True if at least one edge exists, false otherwise.

hasAnyVertex()

Checks if a specific target vertex is a neighbor in any direction.

public hasAnyVertex(string $source, string $target[, array<string|int, mixed> $init = [] ]) : bool
Parameters
$source : string

The '_key' or '_id' of the vertex to start from.

$target : string

The '_key' or '_id' of the target vertex to check for.

$init : array<string|int, mixed> = []

Optional array of query options (e.g., AQL::GRAPH).

Tags
throws
ArangoException
BindException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
ConstantException
Return values
bool

True if the target vertex is a neighbor, false otherwise.

hasInboundVertex()

Checks if a specific target vertex is an inbound neighbor of the start vertex.

public hasInboundVertex(string $to, string $from[, array<string|int, mixed> $init = [] ]) : bool
Parameters
$to : string

The '_key' or '_id' of the vertex to traverse to (start vertex).

$from : string

The '_key' or '_id' of the target vertex to check for.

$init : array<string|int, mixed> = []

Optional array of query options (e.g., AQL::GRAPH).

Tags
throws
ArangoException
BindException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
ConstantException
Return values
bool

True if the target vertex is an inbound neighbor, false otherwise.

hasOutboundVertex()

Checks if a specific target vertex is an outbound neighbor of the start vertex.

public hasOutboundVertex(string $from, string $to[, array<string|int, mixed> $init = [] ]) : bool
Parameters
$from : string

The '_key' or '_id' of the vertex to start from.

$to : string

The '_key' or '_id' of the target vertex to check for.

$init : array<string|int, mixed> = []

Optional array of query options (e.g., AQL::GRAPH).

Tags
throws
ArangoException
BindException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
ConstantException
Return values
bool

True if the target vertex is an outbound neighbor, false otherwise.

countEdgesQuery()

Generates the count query and fill the binds array reference.

protected countEdgesQuery([string|null $from = null ][, string|null $to = null ][, array<string|int, mixed> &$binds = [] ][, array<string|int, mixed> $init = [] ]) : string
Parameters
$from : string|null = null

The from vertex identifier

$to : string|null = null

The to vertex identifier

$binds : array<string|int, mixed> = []

The bindVars array reference.

$init : array<string|int, mixed> = []

The option of the method.

  • 'collection' : The name of the collection, by default use the $this->collection property.
  • 'name' : The name of the bindVariable collection, by default use the @@collection.
  • 'operator' : Indicates if the filter of the vertices use a Logic::AND or Logic::OR operator (default Logic::AND)
  • 'variableName' : The name of the document in the query (default 'doc') AQL::DOC_REF
Tags
throws
BindException
ReflectionException
example
[ $query , $binds ] = $this->countEdgeQuery( $from , $to ) ;
Return values
string

The AQL query expression.

hasVertex()

Private helper to check for vertex existence in a specific direction.

private hasVertex(string $direction, string $startVertex, string $targetVertex[, array<string|int, mixed> $init = [] ]) : bool

This method builds the filter query to check if a $targetVertex exists in the results of a traversal from $startVertex.

Parameters
$direction : string

Traversal::OUTBOUND, Traversal::INBOUND, or Traversal::ANY

$startVertex : string

The _key or _id of the vertex to start from.

$targetVertex : string

The _key or _id of the vertex to find.

$init : array<string|int, mixed> = []

Optional query options.

Tags
throws
ArangoException
BindException
ConstantException
ContainerExceptionInterface
DependencyException
NotFoundException
NotFoundExceptionInterface
ReflectionException
Return values
bool
On this page

Search results