neo4j-php / php-cypher-dsl

A low-level query builder for Cypher written in PHP
MIT License
18 stars 5 forks source link

Reusing nodes in CREATE clause leads to unexpected behaviour #96

Open marijnvanwezel opened 1 year ago

marijnvanwezel commented 1 year ago

Bug report

Cypher's CREATE clause supports reusing variables that were matched elsewhere in the query to create new relationships. For example:

MATCH (charlie:Person {name: 'Charlie Sheen'}), (oliver:Person {name: 'Oliver Stone'})
CREATE (charlie)-[:ACTED_IN {role: 'Bud Fox'}]->(wallStreet:Movie {title: 'Wall Street'})<-[:DIRECTED]-(oliver)

However, this is not properly supported in php-cypher-dsl (see example below).

Code snippet that reproduces the problem

$charlie = node("Person")->withProperties(["name" => "Charlie Sheen"]);
$oliver = node("Person")->withProperties(["name" => "Oliver Stone"]);

$wallStreet = node("Movie")->withProperties(["title" => "Wall Street"]);

$query = query()
    ->match([$charlie, $oliver])
    ->create($charlie->relationshipTo($wallStreet, type: "ACTED_IN", properties: ["role" => "Bud Fox"])->relationshipFrom($oliver))
    ->build();

This gives:

MATCH (:Person {name: 'Charlie Sheen'}), (:Person {name: 'Oliver Stone'}) 
CREATE (:Person {name: 'Charlie Sheen'})-[:ACTED_IN {role: 'Bud Fox'}]->(:Movie {title: 'Wall Street'})<--(:Person {name: 'Oliver Stone'})

Expected output

MATCH (charlie:Person {name: 'Charlie Sheen'}), (oliver:Person {name: 'Oliver Stone'}) 
CREATE (charlie)-[:ACTED_IN {role: 'Bud Fox'}]->(:Movie {title: 'Wall Street'})<--(oliver)

A workaround exists by first retrieving the variable of the node, and then creating a new node with that variable. This is syntactically rather ugly:

$query = query()
    ->match([$charlie, $oliver])
    ->create(node()->withVariable($charlie->getVariable())->relationshipTo($wallStreet, type: "ACTED_IN", properties: ["role" => "Bud Fox"])->relationshipFrom(node()->withVariable($oliver->getVariable())))
    ->build();
beshoo commented 10 months ago

+1

Am facing the same problem