Oihana PHP Arango

aqlInsert.php

Table of Contents

Functions

aqlInsert()  : string
Builds an AQL `INSERT` statement.

Functions

aqlInsert()

Builds an AQL `INSERT` statement.

aqlInsert([array<string|int, mixed> $init = [] ][, array<string|int, mixed>|null &$binds = null ][, string|null $queryID = null ]) : string

This method dynamically constructs an ArangoDB AQL query of the form:

INSERT {document} INTO collection OPTIONS {...} RETURN NEW

It supports binding collection names, attaching additional insert options, and optionally returning the newly inserted document.

The $init array may contain:

  • AQL::DOC (string) — The document to insert, written verbatim (default }).
  • AQL::COLLECTION (string) — The target collection name.
  • AQL::BIND_COLLECTION (string|null) — The NAME of the bind variable that carries the collection, without @ (e.g. AQL::COLLECTION → @@collection). The collection is always bound : when omitted, a name is generated (@@c_<n>). Not a switch — a boolean is refused (BindException).
  • AQL::QUERY_ID (string) — An optional query identifier to prepend the generated name of the bind collection variable.

The document is not formatted here : build it with aqlDocument(), which takes the AQL::RAW_KEYS / AQL::RAW_VALUES / AQL::USE_SPACE options. This function reads none of them.

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

An associative array containing the insert parameters.

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

A reference array to hold bind variables for the query.

$queryID : string|null = null
Tags
throws
BindException

If binding a collection or variable fails.

ReflectionException

If there is an issue reflecting InsertOptions.

example

Example 1 — Insert a single document into a collection

echo aqlInsert
([
    AQL::DOC             => aqlDocument( [ 'name' => 'Eka' , 'age' => 47 ] ) ,
    AQL::COLLECTION      => 'users' ,
    AQL::BIND_COLLECTION => AQL::COLLECTION ,
] , $binds );
// INSERT {name:'Eka',age:47} INTO @@collection RETURN NEW

Insert a document holding a raw AQL expression

echo aqlInsert
([
    AQL::DOC => aqlDocument
    (
        [ '_key' => 'i + 1' , 'name' => 'test' , 'active' => true ] ,
        [ AQL::RAW_KEYS => [ '_key' ] ]
    ) ,
    AQL::COLLECTION      => 'items' ,
    AQL::BIND_COLLECTION => AQL::COLLECTION ,
] , $binds );
// INSERT {_key:i + 1,name:'test',active:true} INTO @@collection RETURN NEW

Insert a nested document with arrays

echo aqlInsert
([
    AQL::DOC => aqlDocument
    (
        [ 'user' => [ 'name' => 'Eka' , 'roles' => [ 'admin' , 'editor' ] ] , 'active' => true ] ,
        [ AQL::USE_SPACE => true ]
    ) ,
    AQL::COLLECTION      => 'users' ,
    AQL::BIND_COLLECTION => AQL::COLLECTION ,
] , $binds );
// INSERT { user:{name:'Eka',roles:['admin','editor']}, active:true } INTO @@collection RETURN NEW
see
InsertOptions

Allows passing advanced options such as overwriteMode, waitForSync, etc.

https://docs.arangodb.com/stable/aql/high-level-operations/insert
since
1.0.0
author

Marc Alcaraz

Return values
string —

The compiled AQL INSERT query string, ready to be executed.

On this page

Search results