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::DOCUMENT (array|object|string) — The document to insert.
  • AQL::COLLECTION (string) — The target collection name.
  • AQL::BIND_COLLECTION (bool) — Whether to bind the collection as a variable.
  • AQL::QUERY_ID (string) — An optional query identifier to prepend the default name of the bind collection variable.
  • AQL::RAW_VALUES (array) — Keys in the document whose values should be treated as raw AQL expressions.
  • AQL::USE_SPACE (bool) — Whether to add spaces around braces and commas for readability.
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::DOCUMENT   => ['name' => 'Eka', 'age' => 47],
    AQL::COLLECTION => 'users'
]);
// INSERT {name:'Eka',age:47} INTO users OPTIONS } RETURN NEW

Insert a document with raw AQL expressions

echo aqlInsert
([
    AQL::DOCUMENT =>
    [
         '_key' => "CONCAT('test', i)",
         'name' => 'test',
         'active' => true
    ],
    AQL::RAW_VALUES => [ '_key' ] ,
    AQL::COLLECTION => 'items'
]);
// INSERT {_key:CONCAT('test', i),name:'test',active:true} INTO items OPTIONS } RETURN NEW

Insert a nested document with arrays

echo aqlInsert
([
    AQL::DOC =>
    [
         'user' => ['name' => 'Eka', 'roles' => ['admin','editor']],
         'active' => true
    ],
    AQL::USE_SPACE => true,
    AQL::COLLECTION => 'users'
]);
// INSERT { user:{name:'Eka',roles:['admin','editor']}, active:true } INTO users OPTIONS } 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