Skip to content

CALL {} (subquery) clause

Marijn van Wezel edited this page Dec 13, 2022 · 8 revisions

The CALL {} clause evaluates a subquery that returns some values. It accepts a query and a list of values to include in the WITH clause for correlation.

Query::call(callable|Query $query, Pattern|string|Variable|(Pattern|string|Variable)[] $variables = []): Query

Parameters

  • $query : Either a decorator taking a Query instance, or a Query instance.
  • $variables : The variables or patterns to include in the WITH clause. Variable names given as strings will automatically be converted to Variable objects.

Relevant methods

  • withSubQuery(Query $subQuery): self : Sets the query to call. This overwrites any previously set sub-query.
  • addWithVariable(Pattern|string|Variable ...$variables): self : Add one or more variables to include in the WITH clause.

Examples

$query = query()
    ->call(static function (Query $query): void
    {
        $query->create(node("Person"));
    })
    ->build();

$this->assertSame("CALL { CREATE (:Person) }", $query);
$subQuery = query()->create(node("Person"));
$query = query()
    ->call($subQuery)
    ->build();

$this->assertSame("CALL { CREATE (:Person) }", $query);
$person = variable();
$query = query()
    ->match(node('Person')->withVariable($person))
    ->call(static function (Query $query) use ($person): void
    {
        $query->remove($person->labeled('Person'));
    }, [$person])
    ->build();

$this->assertStringMatchesFormat("MATCH (%s:Person) CALL { WITH %s REMOVE %s:Person }", $query);

External links