簡體   English   中英

如何通過PHP的Cypher Query訪問標簽集合?

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

我遇到困難時需要提示。 當我觸發Cypher查詢時

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

我在瀏覽器中收到的列為數組,列出了節點“ startNode”具有的所有標簽。

用print_r拋出結果,我得到了目標代碼,盡管我以為我也可以在PHP中將其作為數組。 我試過了

  1. 在經典中使用foreach循環

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

    希望我可以例如通過$ row ['startLabel']抓住它。 這失敗了,因為似乎我得到了一個對象,而不是數組。 然后,我嘗試將其強制轉換為數組或在其上使用get_obj_vars,但同樣失敗。

  2. 我檢查了github中的文檔並發現

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

嘗試了一下,或者得到了整個對象,或者當我嘗試類似的東西時

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

錯誤“致命錯誤:調用未定義的方法Everyman \\ Neo4j \\ Query \\” ...

最后,我想從一個節點獲取標簽(一個或多個),並在PHP中將它們作為數組使用。 我認為這只是一個小問題,但我無法解決。 如果有人暗示我會很高興-謝謝


更新資料

這是我使用的查詢:

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

在Neo4J瀏覽器中,我得到了一個集合(正確):

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

這是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 

如果您需要更多輸出,我也可以發布。

感謝您的支持-非常感謝

我遇到了類似的問題,並通過迭代集合來解決它,就好像它本身就是結果一樣。

例如,如果要構建帶有標簽的數組:

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);
}

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM