Search code examples
neo4jphp

neo4jphp relateTo() to a getResultSet() object


I'm using the Neo4jphp REST API and I'm having a problem of relating a node iv created in my code to a node im retrieving from the neo4j database. I'm retrieving the node as follows

$querystring="MATCH(n)"."WHERE has (n.name) and n.name='Interests'"."RETURN n"; $query=new Everyman\Neo4j\Cypher\Query($client,$querystring); $result=$query->getResultSet();

im creating another node in my code using createNode() $friend=$client->makeNode()->setProperty('name',$fname)->save(); I used relateTo() to relate them

$m=$client->getNode($result);
$m->relateTo($friend,$movi)->save();//$movi is a common movie name

but getting this error

PHP Catchable fatal error:  Object of class Everyman\\Neo4j\\Query\\ResultSet could not be converted to string in /var/www/vendor/everyman/neo4jphp/lib/Everyman/Neo4j/Cache/EntityCache.php 

would really appreciate any input


Solution

  • A neo4jphp query returns a row object (even if theres only one responce) so

    $m=$client->getNode($result);
    

    won't work

    try this instead

    $querystring="MATCH(n) WHERE has (n.name) and n.name='Interests' RETURN n";
    $query=new Everyman\Neo4j\Cypher\Query($client,$querystring);
    $result=$query->getResultSet();
    foreach($result as $resultItem)
    {
       $resultArray[] = $resultItem['x'];
       //$resultItem['x'] is the node object, now $result array is an array of node objects
    }
    $friend= $client->makeNode()->setProperty('name',$fname)->save();
    $resultArray[0]->relateTo($friend,$movi)->save();
    

    this code will also work with this query (easier to read)

    $querystring="MATCH (n {name:"Interests"}) RETURN n";