neoxygen / neo4j-neoclient

Simple PHP HttpClient for the Neo4j ReST API with Multi DB Support
MIT License
121 stars 138 forks source link

Can't extract array of nodes and relationships #44

Closed opb closed 9 years ago

opb commented 9 years ago

Hi there

Sorry if this isn't a bug as such, but I'm having a lot of difficulty simply pulling down all nodes and relationships, so that I can JSON encode this and send to a JS library. I've tried several options, but not hit on the right thing.

// get all nodes and relationships
$query = 'MATCH (n)-[r]->(m) RETURN n,r,m;';
$response = $neo->sendCypherQuery($query);

// this has all the data
$response->getResult();
// but this is an empty object
json_encode($response->getResult()); // {}

// these have subarrays of n, r, and m form the query, but nothing to link them to each other
$response->getRows();

// this looks great, contains all the data...
$response->getResult()->getRelationships();
// ... but json encoding leaves us with empty objects
json_encode($response->getResult()->getRelationships()) // {"19":{},"20":{},"21":{},"22":{}}

I'm trying to follow the data viz guide (http://neo4j.com/developer/guide-data-visualization/). Does the neoclient provide anything out of the box to pull out standard php arrays or json, or do I need to look into running transformers on my data?

Thanks!

ikwattro commented 9 years ago

Hi,

The Result Object provided by the getResult method provide nodes and relationships objects with dedicated methods useful to PHP applications workflow.

You will need to use these methods to create an array corresponding to your needs in order to encode it and send it to your frontend.

Based on the json architecture in the data viz guide :

{"nodes":[{name:"Peter",label:"Person",id:1},{name:"Michael",label:"Person",id:2},
          {name:"Neo4j",label:"Database",id:3}],
 "links":[{source:0, target:1, type:"KNOWS", since:2010},{source:0, target:2, type:"FOUNDED"},
          {source:1, target:2, type:"WORKS_ON"}]}

You'll need to build two arrays, one for the nodes and one for the relationships, for example :

$nodes = [];
$relationships = [];
$query = 'MATCH (n) OPTIONAL MATCH (n)-[r]-() RETURN r,n;';
$result = $client->sendCypherQuery($query)->getResult();
foreach ($result->getNodes() as $node) {
    $nodes[] = [
        'name' => $node->getProperty('name'),
        'label' => $node->getLabels()[0],
        'id' => $node->getId()
    ];
}
foreach ($result->getRelationships() as $rel) {
    $relationships[] = [
        'source' => $rel->getStartNode()->getId(),
        'target' => $rel->getEndNode()->getId(),
        'type' => $rel->getType()
    ];
}

$response = [
    'nodes' => $nodes,
    'links' => $relationships
];
$jsonResponse = json_encode($response);
opb commented 9 years ago

That's fantastic, thanks so much for putting that together! It makes complete sense.

Thanks again.

ikwattro commented 9 years ago

Glad to help, don't hesitate to post your questions on StackOverflow ;-)