Oihana PHP Arango

aqlFor.php

Table of Contents

Functions

aqlFor()  : string
Builds an ArangoDB AQL `FOR` clause, optionally including `SEARCH` and `OPTIONS` segments.

Functions

aqlFor()

Builds an ArangoDB AQL `FOR` clause, optionally including `SEARCH` and `OPTIONS` segments.

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

The generated query follows this canonical form:

FOR <variableName> IN <expression> [SEARCH <searchExpression>] [OPTIONS { ... }]

This function simplifies the construction of AQL loops by automatically compiling the parts (IN, SEARCH, OPTIONS) using helper functions such as aqlSearch() and aqlOptions().

Supported $init keys

Key Type Description
AQL::DOC_REF `string null`
AQL::IN `string null`
AQL::SEARCH `string null`
AQL::OPTIONS `array object

Example: basic usage

echo aqlFor([
    AQL::DOC_REF => 'doc',
    AQL::IN      => 'users'
]);
// → "FOR doc IN users"

Example: with SEARCH and OPTIONS

echo aqlFor
([
    AQL::DOC_REF  => 'u',
    AQL::IN       => 'searchUsers',
    AQL::SEARCH   => 'u.active == true',
    AQL::OPTIONS  =>
    [
        'indexHint'       => 'byActive',
        'forceIndexHint'  => true,
        'disableIndex'    => false,
        'useCache'        => true,
        'lookahead'       => 5
    ]
]);
// → "FOR u IN searchUsers SEARCH u.active == true OPTIONS {\"indexHint\":\"byActive\",\"forceIndexHint\":true,\"disableIndex\":false,\"useCache\":true,\"lookahead\":5}"

Example: using a ForOptions schema object

use oihana\arango\db\options\ForOptions;

$opts = new ForOptions([
    'useCache'  => false,
    'lookahead' => 3
]);

echo aqlFor
`([
    AQL::DOC_REF => 'd',
    AQL::IN      => 'documents',
    AQL::OPTIONS => $opts
]);
// → "FOR d IN documents OPTIONS {\"useCache\":false,\"lookahead\":3}"
Parameters
$init : array<string|int, mixed> = []

An associative array defining the FOR clause elements. Example structure:

[
    AQL::DOC_REF => 'doc',
    AQL::IN      => 'users',
    AQL::SEARCH  => 'doc.age > 30',
    AQL::OPTIONS => [ 'useCache' => true ]
]
Tags
throws
ReflectionException

If hydration of options into ForOptions fails.

see
https://docs.arangodb.com/stable/aql/high-level-operations/for
aqlSearch()
aqlOptions()
since
1.0.0
author

Marc Alcaraz

Return values
string

The complete AQL FOR clause, or an empty string if no valid input is provided.

On this page

Search results