Closed opb closed 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);
That's fantastic, thanks so much for putting that together! It makes complete sense.
Thanks again.
Glad to help, don't hesitate to post your questions on StackOverflow ;-)
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.
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!