Oihana PHP Arango

aqlTraversal.php

Table of Contents

Functions

aqlTraversal()  : string
Builds a full **AQL traversal clause** for ArangoDB queries.

Functions

aqlTraversal()

Builds a full **AQL traversal clause** for ArangoDB queries.

aqlTraversal([array{vertexRef?: string, edgeRef?: ?string, pathRef?: ?string, direction?: string, startVertex?: string, graph?: ?string, edgeCollection?: array|string|null, minDepth?: int|null, maxDepth?: int|null, prune?: string|array|null, options?: array|object|string|null} $init = [] ][, array<string|int, mixed>|null &$binds = null ]) : string

This helper constructs the canonical Arango Query Language (AQL) traversal expression, optionally including depth ranges, edge collections or graph names, direction, and bind variables.

It supports flexible initialization via the $init array, automatic bind variable injection, and seamless hydration of traversal options via TraversalOptions.

🧩 Canonical Form

FOR <vertexRef>, <edgeRef>, <pathRef>
IN <minDepth>..<maxDepth> <direction> <startVertex>
GRAPH <graphName>
OPTIONS { ... }

or, when using edge collections instead of a graph:

FOR <vertexRef>, <edgeRef>, <pathRef>
IN <minDepth>..<maxDepth> <direction> <startVertex>
<edgeCollection1>, <edgeCollection2>, ...

🔒 Bind Variables

If $binds is provided, both AQL::GRAPH and AQL::START_VERTEX (and optionally AQL::EDGE_COLLECTION) are automatically bound using aqlBind(), ensuring safe, injection-free query generation.

Example of secure binding:

$binds = [];
$aql = aqlTraversal([
AQL::GRAPH        => 'socialGraph',
AQL::START_VERTEX => '@start',
AQL::DIRECTION    => Traversal::INBOUND
], $binds);

print_r($binds);
// ['@start' => 'users/123', '@graph' => 'socialGraph']

💡 Usage Examples

1 - Simple graph traversal

echo aqlTraversal
([
    AQL::GRAPH         => 'socialGraph',
    AQL::START_VERTEX  => 'users/123',
]);
// FOR vertex IN OUTBOUND 'users/123' GRAPH 'socialGraph'

2 - Traversal with edges and path references

echo aqlTraversal
([
    AQL::VERTEX_REF    => 'v',
    AQL::EDGE_REF      => 'e',
    AQL::PATH_REF      => 'p',
    AQL::DIRECTION     => Traversal::INBOUND,
    AQL::GRAPH         => 'organization',
    AQL::START_VERTEX  => 'employees/42',
]);
// FOR v, e, p IN INBOUND 'employees/42' GRAPH 'organization'

3 - Depth-limited traversal

echo aqlTraversal
([
    AQL::GRAPH         => 'socialGraph',
    AQL::START_VERTEX  => 'users/123',
    AQL::MIN_DEPTH     => 1,
    AQL::MAX_DEPTH     => 3,
]);
// FOR vertex IN 1..3 OUTBOUND 'users/123' GRAPH 'socialGraph'

4 - Traversal using multiple edge collections

echo aqlTraversal
([
    AQL::EDGE_COLLECTION => ['follows', 'likes'],
    AQL::START_VERTEX    => 'users/123',
    AQL::DIRECTION       => Traversal::OUTBOUND,
]);
// FOR vertex IN OUTBOUND 'users/123' follows, likes

5 - With PRUNE condition

echo aqlTraversal
([
    AQL::GRAPH         => 'socialGraph',
    AQL::START_VERTEX  => 'users/123',
    AQL::PRUNE         => 'vertex.age < 18',
]);
// FOR vertex IN OUTBOUND 'users/123' GRAPH 'socialGraph' PRUNE vertex.age < 18

6 - With OPTIONS

echo aqlTraversal
([
    AQL::GRAPH         => 'companyGraph',
    AQL::START_VERTEX  => 'departments/1',
    AQL::OPTIONS       => ['bfs' => true, 'uniqueVertices' => 'global'],
]);
// FOR vertex IN OUTBOUND 'departments/1' GRAPH 'companyGraph' OPTIONS { "bfs": true, "uniqueVertices": "global" }
Parameters
$init : array{vertexRef?: string, edgeRef?: ?string, pathRef?: ?string, direction?: string, startVertex?: string, graph?: ?string, edgeCollection?: array|string|null, minDepth?: int|null, maxDepth?: int|null, prune?: string|array|null, options?: array|object|string|null} = []

Configuration for the traversal expression.

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

Optional reference to a bind variable array; used for safe variable substitution.

Tags
throws
BindException

If a variable binding fails.

ConstantException

If the direction is not a valid Traversal constant.

ReflectionException

If aqlOptions() hydration fails due to reflection issues.

since
1.0.0
author

Marc Alcaraz

see
https://docs.arangodb.com/stable/aql/graphs/traversals/
Return values
string

The generated AQL traversal clause, or an empty string if input is invalid.

On this page

Search results