How can I access the label collection from a Cypher Query in PHP?

728 views Asked by At

I need a hint as I am stuck. When I fire my Cypher Query

MATCH (startNode) -[r]- (zielNode) 
return labels(startNode) as startLabel

I receive in the browser the column as array, listing all labels the node "startNode" has.

Throwing the result with print_r I get object code, though I was assuming I can also grab this as array in PHP. I tried

  1. Using a foreach loop with classical

    foreach ($result as $row) {  ...  } 
    

    hoping I could grab it e.g. by $row['startLabel']. This failed as it seems I get an object back, not an array. I tried then to cast it as array or use get_obj_vars on it, but that failed as well.

  2. I checked the documentation in github and found

    $node = $client->getNode('startNode);
    $nodeLabels = $client->getLabels($node);
    

tried it and got either the whole object or when I try something like

    $label = $row[x]->getLabels($node);     

an error "Fatal error: Call to undefined method Everyman\Neo4j\Query\" ...

In the end I want to have the labels (one or many) from a node and work with them in PHP as array. I think its only a little issue but I cant get to the solution. If someone has a hint I would be happy - thanks


Update

Here is the query I use:

MATCH (startNode {uuid:"554b4e5e8fb38"}) -[r]- (targetNode) return labels(targetNode) as targetLabel

In the Neo4J Browser I get a collection (correctly):

 targetLabel
 [Group, SUB1]
 [Group, SUB2]
 [Group, SUB2]
 [Group, Local]

Here is the PHP code:

 $queryString = '        
 MATCH (startNode {uuid:"554b4e5e8fb38"}) -[r]- (targetNode) return   labels(targetNode) as targetLabel
 ';   

 $query = new Everyman\Neo4j\Cypher\Query($client, $queryString);
 $result = $query->getResultSet();        

 foreach ($result as $row) { 

    echo gettype($row['targetLabel']);
    var_dump($row['targetLabel']);
}

  "gettype" says that "$row['targetLabel']" is an object. A var_dump gives    
  this result (I cutted it down as its a long output):

object(Everyman\Neo4j\Query\Row)#16 (5) { ["client":protected]=> object(Everyman\Neo4j\Client)#2 (8) { ["transport":protected]=> object(Everyman\Neo4j\Transport\Curl)#3 (7) { ["handle":protected]=> resource(27) of type (curl) ["scheme":protected]=> string(4) "http" ["host":protected]=> string(9) "localhost" ["port":protected]=> int(7474) ["path":protected]=> string(8) "/db/data" ["username":protected]=> string(5) "xx" ["password":protected]=> string(6) "xx" } ["entityMapper":protected]=> object(Everyman\Neo4j\EntityMapper)#11 (1) { ["client":protected]=> *RECURSION* } ["entityCache":protected]=> object(Everyman\Neo4j\Cache\EntityCache)#12 (3) { ["client":protected]=> *RECURSION* ["cache":protected]=> object(Everyman\Neo4j\Cache\Null)#13 (0) { } ["cacheTimeout":protected]=> int(0) } ["labelCache":protected]=> object(Everyman\Neo4j\Cache\Variable)#6 (1) { ["items":protected]=> array(0) { } } ["serverInfo":protected]=> array(14) { ["extensions"]=> array(0) { } ["node"]=> string(34) "http://localhost:7474/db/data/node" ["node_index"]=> string(40) "http://localhost:7474/db/data/index/node" ["relationship_index"]=> string(48) "http://localhost:7474/db/data/index/relationship" ["extensions_info"]=> string(33) "http://localhost:7474/db/data/ext" ["relationship_types"]=> string(48) "http://localhost:7474/db/data/relationship/types" ["batch"]=> string(35) "http://localhost:7474/db/data/batch" ["cypher"]=> string(36) "http://localhost:7474/db/data/cypher" ["indexes"]=> string(42) "http://localhost:7474/db/data/schema/index" ["constraints"]=> string(47) "http://localhost:7474/db/data/schema/constraint" ["transaction"]=> string(41) "http://localhost:7474/db/data/transaction" ["node_labels"]=> string(36) "http://localhost:7474/db/data/labels" ["neo4j_version"]=> string(5) "2.2.1" ["version"]=> array(4) { ["full"]=> string(5) "2.2.1" ["major"]=> string(1) "2" ["minor"]=> string(1) "2" ["release"]=> string(1) "1" } } ["openBatch":protected]=> NULL ["nodeFactory":protected]=> object

If you need more output I can post it as well.

Thanks for your support - very appreciated

1

There are 1 answers

0
grovina On

I had a similar problem and solved it by iterating the collection as if it was a result itself.

For example, if you want to build an array with the labels:

foreach ($result as $row) {
  $labels = array();

  foreach ($row['startLabel'] as $label) {
    //Here $label works just like $row in the outer foreach, you could access it's subfields (if it had any) with $label['subfield']
    $labels[] = $label;
  }

  print_r($labels);
}

Hope it helps you.