Skip to content

Commit e3b962c

Browse files
committed
updated examples
1 parent c0129c4 commit e3b962c

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

examples/document.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66

77

88
try {
9-
$connection = new Connection($connectionOptions);
10-
$handler = new DocumentHandler($connection);
9+
$connection = new Connection($connectionOptions);
10+
$collectionHandler = new CollectionHandler($connection);
11+
$handler = new DocumentHandler($connection);
12+
13+
// set up a document collection "users"
14+
$collection = new Collection("users");
15+
try {
16+
$collectionHandler->add($collection);
17+
}
18+
catch (\Exception $e) {
19+
// collection may already exist - ignore this error for now
20+
}
1121

1222
// create a new document
1323
$user = new Document();
1424
$user->set("name", "John");
1525
$user->age = 19;
1626

17-
$id = $handler->add("users", $user);
27+
$id = $handler->save("users", $user);
1828

1929
// get documents by example
2030
$cursor = $handler->getByExample("users", array("name" => "John", "age" => 19));
@@ -30,7 +40,7 @@
3040
$user->level = 1;
3141
$user->vists = array(1, 2, 3);
3242

33-
$id = $handler->add("users", $user);
43+
$id = $handler->save("users", $user);
3444
var_dump("CREATED A NEW DOCUMENT WITH ID: ", $id);
3545

3646
// get this document from the server

examples/edge.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace triagens\ArangoDb;
4+
5+
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'init.php';
6+
7+
8+
try {
9+
$connection = new Connection($connectionOptions);
10+
$collectionHandler = new CollectionHandler($connection);
11+
$documentHandler = new DocumentHandler($connection);
12+
$edgeHandler = new EdgeHandler($connection);
13+
14+
// set up two document collections
15+
$collection = new Collection("employees");
16+
try {
17+
$collectionHandler->add($collection);
18+
}
19+
catch (\Exception $e) {
20+
// collection may already exist - ignore this error for now
21+
}
22+
23+
$collection = new Collection("departments");
24+
try {
25+
$collectionHandler->add($collection);
26+
}
27+
catch (\Exception $e) {
28+
// collection may already exist - ignore this error for now
29+
}
30+
31+
// set up an edge collection to link the two previous collections
32+
$collection = new Collection("worksFor");
33+
$collection->setType(3);
34+
35+
try {
36+
$collectionHandler->add($collection);
37+
}
38+
catch (\Exception $e) {
39+
// collection may already exist - ignore this error for now
40+
}
41+
42+
// create a new department
43+
$marketing = Document::createFromArray(array("name" => "Marketing"));
44+
$documentHandler->save("departments", $marketing);
45+
46+
// create another department
47+
$finance = Document::createFromArray(array("name" => "Finance"));
48+
$documentHandler->save("departments", $finance);
49+
50+
// create a new employee
51+
$john = Document::createFromArray(array("name" => "John"));
52+
$documentHandler->save("employees", $john);
53+
54+
// create another employee
55+
$jane = Document::createFromArray(array("name" => "Jane"));
56+
$documentHandler->save("employees", $jane);
57+
58+
// now insert a link between Marketing and Jane
59+
$worksFor = Edge::createFromArray(array("startDate" => "2009-06-23", "endDate" => "2014-11-12"));
60+
$edgeHandler->saveEdge("worksFor", $marketing->getHandle(), $jane->getHandle(), $worksFor);
61+
62+
// now insert a link between Finance and Jane
63+
$worksFor = Edge::createFromArray(array("startDate" => "2014-11-12"));
64+
$edgeHandler->saveEdge("worksFor", $finance->getHandle(), $jane->getHandle(), $worksFor);
65+
66+
// now insert a link between Finance and John
67+
$worksFor = Edge::createFromArray(array("startDate" => "2012-04-01"));
68+
$edgeHandler->saveEdge("worksFor", $finance->getHandle(), $john->getHandle(), $worksFor);
69+
70+
71+
} catch (ConnectException $e) {
72+
print $e . PHP_EOL;
73+
} catch (ServerException $e) {
74+
print $e . PHP_EOL;
75+
} catch (ClientException $e) {
76+
print $e . PHP_EOL;
77+
}

examples/graph.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
$graph->set('_key', 'Graph');
1414
$graph->setVerticesCollection('VertexCollection');
1515
$graph->setEdgesCollection('EdgeCollection');
16+
17+
try {
18+
$graphHandler->dropGraph($graph);
19+
}
20+
catch (\Exception $e) {
21+
// graph may not yet exist. ignore this error for now
22+
}
23+
1624
$graphHandler->createGraph($graph);
1725

1826
// Define some arrays to build the content of the vertices and edges
@@ -46,7 +54,7 @@
4654
var_dump($graphHandler->hasVertex('Graph', 'vertex1'));
4755

4856
// Save the connecting edge
49-
$saveEdgeResult1 = $graphHandler->saveEdge('Graph', 'vertex1', 'vertex2', 'somelabelValue', $edge1);
57+
$saveEdgeResult1 = $graphHandler->saveEdge('Graph', $vertex1->getHandle(), $vertex2->getHandle(), 'somelabelValue', $edge1);
5058

5159
// check if edge exists
5260
var_dump($graphHandler->hasEdge('Graph', 'edge1'));
@@ -57,7 +65,8 @@
5765
// Remove vertices and edges
5866
$result1 = $graphHandler->removeVertex('Graph', 'vertex1');
5967
$result1 = $graphHandler->removeVertex('Graph', 'vertex2');
60-
$result1 = $graphHandler->removeEdge('Graph', 'edge1');
68+
69+
// the connecting edge will be deleted automatically
6170
} catch (ConnectException $e) {
6271
print $e . PHP_EOL;
6372
} catch (ServerException $e) {

lib/triagens/ArangoDb/Collection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,15 @@ class Collection
195195
/**
196196
* Constructs an empty collection
197197
*
198+
* @param string $name - name for the collection
199+
*
198200
* @return Collection
199201
*/
200-
public function __construct()
202+
public function __construct($name = null)
201203
{
204+
if ($name !== null) {
205+
$this->setName($name);
206+
}
202207
}
203208

204209
/**

0 commit comments

Comments
 (0)