简体   繁体   中英

how can i create relationships between nodes using php and neo4j

I am creating nodes through a form along with php and the parameters are taken by POST to the neo4j database. What I want to do is create a relationship between nodes when I enter a person node so that the relationship is created automatically and only once.

$nombre = $_POST["nombre"];
$cedula = $_POST["cedula"];
$edad = $_POST["edad"];
$cedulaP = $_POST["cedulaP"];
$cedulaM = $_POST["cedulaM"];
$genero = $_POST["genero"];

$results = $client->runStatements([
        Statement::create('CREATE (p:Persona {nombre: $nombre, cedula: $cedula, edad: $edad, 
         cedulaP: $cedulaP, cedulaM: $cedulaM, genero: $genero })', 
        ['nombre' => $nombre, 'cedula' => $cedula, 'edad' => $edad, 'cedulaP' => $cedulaP, 
        'cedulaM' => $cedulaM, 'genero' => $genero]),

When entering a new person node through the form, I want it to create the relationship as long as the variable cedulaP is equal to cedula (this cedula is from the parent node). It is a kind of family tree. The problem I have is that when I enter a new node it gives me an error.

$results1 = $client->run('MATCH (a:Persona),(b:Persona) WHERE a.cedulaP = b.cedula CREATE (b)-[r:PADRE]->(a)');

I think my syntax is wrong, someone who can help me please

I think it's not related to PHP library. You can make both queries in one and you can simplify node properties as dictionary.

$nombre = $_POST["nombre"];
$cedula = $_POST["cedula"];
$edad = $_POST["edad"];
$cedulaP = $_POST["cedulaP"];
$cedulaM = $_POST["cedulaM"];
$genero = $_POST["genero"];

$properties = ['nombre' => $nombre, 'cedula' => $cedula, 'edad' => $edad, 'cedulaP' => $cedulaP, 'cedulaM' => $cedulaM, 'genero' => $genero];

$statement = new Statement('CREATE (p:Persona $props) 
WITH p 
MATCH (b:Persona) WHERE p.cedulaP = b.cedula 
CREATE (b)-[r:PADRE]->(a)', [$props => $properties]);
$client->runStatement($statement, 'default');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM