如何从PHP中的Cypher Query访问标签集合?

时间:2022-03-22 18:02:35

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

因为我被卡住了,我需要一个提示。当我解雇我的Cypher查询时

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.

我在浏览器中收到列为数组,列出节点“startNode”具有的所有标签。

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

使用print_r抛出结果我得到了对象代码,虽然我假设我也可以在PHP中将其作为数组获取。我试过了

  1. Using a foreach loop with classical

    使用经典的foreach循环

    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.

    希望我能抓住它,例如通过$ row ['startLabel']。这失败了,因为我似乎得到了一个对象,而不是一个数组。我尝试将其转换为数组或使用get_obj_vars,但也失败了。

  2. I checked the documentation in github and found

    我检查了github中的文档并找到了

    $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\" ...

错误“致命错误:调用未定义的方法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

最后,我希望从一个节点获得标签(一个或多个),并在PHP中使用它们作为数组。我认为这只是一个小问题,但我无法得到解决方案。如果有人有提示我会很高兴 - 谢谢


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):

在Neo4J浏览器中,我得到了一个集合(正确):

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

Here is the PHP code:

这是PHP代码:

 $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 个解决方案

#1


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.

希望它能帮到你。

#1


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.

希望它能帮到你。