forked from electric-al/Neo4J-REST-PHP-API-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.php
More file actions
91 lines (70 loc) · 2.19 KB
/
Copy pathdemo.php
File metadata and controls
91 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Include the API PHP file
*/
require('php-neo-rest.php');
/**
* Create a graphDb connection
* Note: this does not actually perform any network access,
* the server is only accessed when you use the database
*/
$graphDb = new GraphDatabaseService('http://localhost:7474/');
/**
* Lets create some nodes
* Note: Unlike the java API, these nodes are NOT saved until you call the save() method (see below)
*/
$firstNode = $graphDb->createNode();
$secondNode = $graphDb->createNode();
$thirdNode = $graphDb->createNode();
/**
* Assign some attributes to the nodes and save the,
*/
$firstNode->message = "Hello, ";
$firstNode->blah = "blah blah";
$firstNode->save();
$firstNode->blah = NULL; // Setting to null removes the property
$firstNode->save();
$secondNode->message = "world!";
$secondNode->someOtherAttribute = 'blah blah blah';
$secondNode->save();
$thirdNode->message = "third node";
$thirdNode->save();
/**
* Create a relationship between some nodes. These can also have attributes.
* Note: Relationships also need to be saved before they exist in the DB.
*/
$relationship = $firstNode->createRelationshipTo($secondNode, 'KNOWS');
$relationship->message = "brave Neo4j";
$relationship->blah = "blah blah";
$relationship->save();
$relationship->blah = NULL; // Setting to NULL removed the property
$relationship->save();
$relationship2 = $thirdNode->createRelationshipTo($secondNode, 'LOVES');
$relationship2->save();
/**
* Dump each node we created
*/
dump_node($firstNode);
dump_node($secondNode);
dump_node($thirdNode);
/**
* Perform Cypher Query
*/
$script = 'START a = ('.$secondNode->getId().') MATCH (a)<-->(x) RETURN x';
$res = $graphDb->performCypherQuery($script);
var_dump($res);
/**
* A little utility function to display a node
*/
function dump_node($node)
{
$rels = $node->getRelationships();
echo 'Node '.$node->getId()."\t\t\t\t\t\t\t\t".json_encode($node->getProperties())."\n";
foreach($rels as $rel)
{
$start = $rel->getStartNode();
$end = $rel->getEndNode();
echo " Relationship ".$rel->getId()." : Node ".$start->getId()." ---".$rel->getType()."---> Node ".$end->getId(),
"\t\t\t\t\t\t\t\t".json_encode($rel->getProperties())."\n";
}
}