We are translating a Neo4j cypher query to PHP cypher DSL (Domain-Specific Language) using the php-cypher-dsl library to have more flexibility.
We struggle with this because the library's documentation is not really explaining how to use it in much detail. Given that we have a plain query in cypher Neo4j:
MATCH (a:Resource), (b:Resource)
WHERE a.uri = $uri AND b.uri = $object
CREATE (a)-[r:{$propertyUri}]->(b)
RETURN type(r)
How can we translate this Neo4j cypher query to PHP cypher DSL?
The first thing you need to do is create all the nodes that you want to match on, as well as the parameters you want to use, like so:
The
nodefunction takes as its only argument the label of the node and theparameterfunction the name of the parameter. Next, you can start to compose your query. You can start a new query using thequeryfunction, like so:This returns a
Queryobject, which has a number of methods to add new clauses to your query, such asmatch,set,whereandcreate. These correspond to the clauses that Cypher offers.We start by matching on the nodes
$aand$b:Next, we want to create the condition to match on. php-cypher-dsl comes with a builder pattern for creating such expressions:
Next, before we create our
CREATEclause, we need to create a new variable$rthat gets shared between ourCREATEandRETURNclause:To create the
CREATEclause, we need to perform some trickery to remove the:Resourcelabel from our nodes again. This part of the API still needs some improvement. We can get a new node with the same variable by usingnode()->withVariable($a->getVariable()). All together, that looks like:Finally, we can return the value we want:
All put together, the code looks like this:
Which I think is much less readable than using a normal query with variables. If you don't require complex logic to build queries (and only use pre-created queries, such as the one in your question), I'd recommend you to not use this library.