简体   繁体   English

使用PHP从Neo4j数据库导出json文件

[英]Export json file from Neo4j database using PHP

For that, I used the following code: 为此,我使用了以下代码:

$result = $client->sendCypherQuery('MATCH (n) RETURN n')->getResult();
$nodes = [];
foreach ($result->getNodes() as $node) {
    $nodes[] = [
        'id' => $node->getId(),
        'labels' => $node->getLabels(),
        'properties' => $node->getProperties()
    ];
}

var_dump(json_encode($nodes));

But I get the following error: 但是我收到以下错误:

Fatal error: Call to a member function getNodes() on a non-object in C:\\wamp\\www\\PhpProjectNeo4j1\\index.php 致命错误:在C:\\ wamp \\ www \\ PhpProjectNeo4j1 \\ index.php中的非对象上调用成员函数getNodes()

You are trying to call a function on an object which is empty or does not exists. 您试图在一个空的或不存在的对象上调用一个函数。 First print the result of the returned query. 首先打印返回查询的结果。

Probably the query does not return any result, but first you need to connect to the neo4j database : 该查询可能不会返回任何结果, 但是首先您需要连接到neo4j数据库

Here is an example of how you can do it: 以下是如何执行此操作的示例:

https://github.com/neo4j-examples/movies-php-neoclient/blob/fe5fd9222945ca8e9efc4ab3cac6b743f2ff8b24/index.php https://github.com/neo4j-examples/movies-php-neoclient/blob/fe5fd9222945ca8e9efc4ab3cac6b743f2ff8b24/index.php

$client = ClientBuilder::create()
    ->addConnection('default', $cnx['scheme'], $cnx['host'], $cnx['port'], true, $cnx['user'], $cnx['pass'])
    ->setAutoFormatResponse(true)
    ->setDefaultTimeout(20)
    ->build();

$result = $client->sendCypherQuery('MATCH (n) RETURN n')->getResult();
var_dump($result);    

A common way is to test the returned result emptiness before traversing the array. 一种常见的方法是在遍历数组之前测试返回的结果是否为空。

if (!empty($result)) {
    foreach ($result->getNodes() as $node) {
        $nodes[] = [
            'id' => $node->getId(),
            'labels' => $node->getLabels(),
            'properties' => $node->getProperties()
        ];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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