forked from arangodb/arangodb-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.php
75 lines (59 loc) · 2.2 KB
/
graph.php
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
<?php
namespace ArangoDBClient;
// get connection options from a helper file
require __DIR__ . '/init.php';
try {
// Setup connection, graph and graph handler
$connection = new Connection($connectionOptions);
$graphHandler = new GraphHandler($connection);
$graph = new Graph();
$graph->set('_key', 'Graph');
$graph->addEdgeDefinition(EdgeDefinition::createUndirectedRelation('EdgeCollection', 'VertexCollection'));
try {
$graphHandler->dropGraph($graph);
} catch (\Exception $e) {
// graph may not yet exist. ignore this error for now
}
$graphHandler->createGraph($graph);
// Define some arrays to build the content of the vertices and edges
$vertex1Array = [
'_key' => 'vertex1',
'someKey1' => 'someValue1'
];
$vertex2Array = [
'_key' => 'vertex2',
'someKey2' => 'someValue2'
];
$edge1Array = [
'_key' => 'edge1',
'someEdgeKey1' => 'someEdgeValue1'
];
// Create documents for 2 vertices and a connecting edge
$vertex1 = Vertex::createFromArray($vertex1Array);
$vertex2 = Vertex::createFromArray($vertex2Array);
$edge1 = Edge::createFromArray($edge1Array);
// Save the vertices
$graphHandler->saveVertex('Graph', $vertex1);
$graphHandler->saveVertex('Graph', $vertex2);
// Get the vertices
$graphHandler->getVertex('Graph', 'vertex1');
$graphHandler->getVertex('Graph', 'vertex2');
// check if vertex exists
var_dump($graphHandler->hasVertex('Graph', 'vertex1'));
// Save the connecting edge
$graphHandler->saveEdge('Graph', $vertex1->getHandle(), $vertex2->getHandle(), 'somelabelValue', $edge1);
// check if edge exists
var_dump($graphHandler->hasEdge('Graph', 'edge1'));
// Get the connecting edge
$graphHandler->getEdge('Graph', 'edge1');
// Remove vertices and edges
$graphHandler->removeVertex('Graph', 'vertex1');
$graphHandler->removeVertex('Graph', 'vertex2');
// the connecting edge will be deleted automatically
} catch (ConnectException $e) {
print $e . PHP_EOL;
} catch (ServerException $e) {
print $e . PHP_EOL;
} catch (ClientException $e) {
print $e . PHP_EOL;
}