Oihana PHP Arango

EdgesCountTrait uses trait:short

Provides utilities for counting relationships (edges) and unique neighbors (vertices) in an ArangoDB edge collection.

This trait extends ArangoTrait and VerticesTrait and offers two distinct counting strategies:

1. Edge Counting (via countEdges)

What it does: Counts the actual edge documents that match a filter (e.g., _from == 'A' AND _to == 'B'). It uses RETURN LENGTH(FOR ... FILTER ...)

Use case: Answers "How many times does relation X exist?"

Example: If 'user/1' follows 'user/2' twice (two edge documents), countEdges('user/1', 'user/2') will return 2.

2. Vertex Counting (via countVertices, countOutboundVertices, etc.)

What it does: Counts the unique vertex documents reached by a traversal (OUTBOUND, INBOUND, ANY). It uses FOR ... COLLECT WITH COUNT.

Use case: Answers "How many unique neighbors does vertex X have?"

Example:

If 'user/1' follows 'user/2' twice, countOutboundVertices('user/1') will return 1 (as 'user/2' is one unique vertex).

Usage

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

// How many edge documents connect 'users/1' to 'posts/5'?
$edgeCount = $edges->countEdges('users/1', 'posts/5');

// How many unique users does 'users/1' follow?
$vertexCount = $edges->countOutboundVertices('users/1');
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.
prepareTraversal()  : array{0: array, 1: string|array|null, 2: string|null, 3: string|null, 4: string}
Prepares traversal parameters and returns key elements required to execute a vertex traversal query.
countEdgesQuery()  : string
Generates the count query and fill the binds array reference.
prepareTraversalWith()  : string
Derives the AQL `WITH` clause declaring the vertex collections reached by an **anonymous** traversal (edge collection, no named graph).

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.

prepareTraversal()

Prepares traversal parameters and returns key elements required to execute a vertex traversal query.

public prepareTraversal(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 = [] ]) : array{0: array, 1: string|array|null, 2: string|null, 3: string|null, 4: string}

This method validates the direction, computes the full vertex ID, prepares bind variables, and sets default edge or graph collections if not provided. It is a utility for methods that fetch or count vertices via Traversal in an ArangoDB edge collection.

Parameters
$direction : string

The direction of traversal: Traversal::OUTBOUND, Traversal::INBOUND, or Traversal::ANY.

$vertex : string|null = null

Optional '_key' or '_id' of the vertex to start the traversal from. If null, the default $this->from or $this->to is used depending on the direction.

$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 reference to array of query initialization and configuration.

Tags
throws
ConstantException

If the $direction is not a valid Traversal value.

InvalidArgumentException

If the resolved vertex ID is null or empty.

example
[$binds, $filter, $from, $to, $vertexId] = $this->prepareTraversal(
    Traversal::OUTBOUND,
    '123',
    ['graph' => 'my_graph']
);
Return values
array{0: array, 1: string|array|null, 2: string|null, 3: string|null, 4: string}

Returns an array containing: 0 => prepared bind variables, 1 => filter expression(s), 2 => "from" collection/vertex, 3 => "to" collection/vertex. 4 => resolved vertex ID,

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.

prepareTraversalWith()

Derives the AQL `WITH` clause declaring the vertex collections reached by an **anonymous** traversal (edge collection, no named graph).

private prepareTraversalWith(string $direction[, Documents|null $from = null ][, Documents|null $to = null ][, array<string|int, mixed> $init = [] ]) : string

In a cluster, collections accessed dynamically by collection-set traversals must be declared up front so they are all locked at query start, which avoids deadlocks. Named-graph traversals already know their collections, so this returns an empty string for them. The clause is harmless (a no-op) on a single server.

Declared collections, by direction:

An explicit AQL::WITH entry in $init (a collection name or an array of names) overrides the direction-based derivation.

Parameters
$direction : string

Traversal direction (Traversal).

$from : Documents|null = null

The _from vertex model (may be null).

$to : Documents|null = null

The _to vertex model (may be null).

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

The traversal init array (read-only here).

Tags
see
https://docs.arangodb.com/stable/aql/high-level-operations/with/
Return values
string

The WITH coll1, coll2, ... clause, or an empty string when there is nothing to declare (named graph, missing models, or explicit empty).

On this page

Search results