Skip to content

Commit 17ff363

Browse files
committed
added API methods to allow more flexible updating and deleting
1 parent e10e617 commit 17ff363

File tree

4 files changed

+79
-39
lines changed

4 files changed

+79
-39
lines changed

examples/collection.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@
88
$connection = new Connection($connectionOptions);
99
$handler = new CollectionHandler($connection);
1010

11+
// create a new collection
12+
$col = new Collection();
13+
$col->setName("hihi");
14+
$result = $handler->add($col);
15+
var_dump($result);
16+
17+
// get an existing collection
18+
$result = $handler->get("hihi");
19+
var_dump($result);
20+
1121
// get an existing collection
12-
$result = $handler->get("peng");
22+
$result = $handler->get("hihi");
1323
var_dump($result);
1424

1525
// get number of documents from an existing collection
16-
$result = $handler->getCount("peng");
26+
$result = $handler->getCount("hihi");
1727
var_dump($result);
1828

1929
// get figures for an existing collection
20-
$result = $handler->getFigures("peng");
30+
$result = $handler->getFigures("hihi");
2131
var_dump($result);
2232

23-
// create a new collection
24-
$col = new Collection();
25-
$col->setName("hihi2");
26-
$result = $handler->add($col);
27-
var_dump($result);
28-
29-
// get an existing collection
30-
$result = $handler->get("hihi2");
31-
var_dump($result);
32-
3333
// delete the collection
34-
$result = $handler->delete("hihi2");
34+
$result = $handler->delete("hihi");
3535
var_dump($result);
3636

3737
// rename a collection
38-
// $handler->rename($col,"hihi30");
38+
// $handler->rename($col, "hihi30");
3939

4040
// truncate an existing collection
41-
// $result = $handler->truncate("foo");
41+
// $result = $handler->truncate("hihi");
4242
}
4343
catch (ConnectException $e) {
4444
var_dump($e->getMessage());

examples/document.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@
1414
var_dump($result);
1515

1616
// get the ids of all documents in the collection
17-
$result = $handler->getAllIds("fux");
17+
$result = $handler->getAllIds("users");
1818
var_dump($result);
1919

2020
// create a new document
21-
$document = new Document();
22-
$document->set("name", "fux");
23-
$document->level = 1;
24-
$document->vists = array(1, 2, 3);
21+
$user = new Document();
22+
$user->set("name", "users");
23+
$user->level = 1;
24+
$user->vists = array(1, 2, 3);
2525

26-
$id = $handler->add("fux", $document);
26+
$id = $handler->add("users", $user);
2727
var_dump("CREATED A NEW DOCUMENT WITH ID: ", $id);
2828

2929
// get this document from the server
30-
$result = $handler->get("fux", $id);
31-
var_dump($result);
30+
$userFromServer = $handler->getById("users", $id);
31+
var_dump($userFromServer);
3232

3333
// update this document
34-
$document->nonsense = "hihi";
35-
unset($document->name);
36-
$result = $handler->update("fux", $document);
34+
$userFromServer->nonsense = "hihi";
35+
unset($userFromServer->name);
36+
$result = $handler->update($userFromServer);
3737
var_dump($result);
3838

3939
// get the updated document back
40-
$result = $handler->get("fux", $id);
40+
$result = $handler->get("users", $id);
4141
var_dump($result);
4242

4343
// delete the document
44-
$result = $handler->delete("fux", $id);
44+
$result = $handler->deleteById("users", $id);
4545
var_dump($result);
4646

4747
}

examples/select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/* set up some example statements */
88
$statements = array(
9-
"select f from fux f" => array(),
9+
"select u from users u" => array(),
1010
"select u from users u" => NULL,
1111
"select u from users u where u.id == 1 +@id@" => array("id" => 6),
1212
"select u from users u where u.id == 2+ @id@" => array("id" => 6),

lib/triagens/Avocado/DocumentHandler.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@ class DocumentHandler extends Handler {
3535

3636
/**
3737
* Get a single document from a collection
38-
* This will throw if the document cannot be fetched from the server
38+
* Alias method for getById()
3939
*
4040
* @throws Exception
4141
* @param mixed $collectionId - collection id as a string or number
4242
* @param mixed $documentId - document identifier
4343
* @return Document - the document fetched from the server
4444
*/
4545
public function get($collectionId, $documentId) {
46+
return $this->getById($collectionId, $documentId);
47+
}
48+
49+
/**
50+
* Get a single document from a collection
51+
* This will throw if the document cannot be fetched from the server
52+
*
53+
* @throws Exception
54+
* @param mixed $collectionId - collection id as a string or number
55+
* @param mixed $documentId - document identifier
56+
* @return Document - the document fetched from the server
57+
*/
58+
public function getById($collectionId, $documentId) {
4659
$url = UrlHelper::buildUrl(Urls::URL_DOCUMENT, $collectionId, $documentId);
4760
$response = $this->getConnection()->get($url);
4861

@@ -151,20 +164,35 @@ public function add($collectionId, Document $document, $create = NULL) {
151164
}
152165

153166
/**
154-
* Update an existing document in a collection
167+
* Update an existing document in a collection, identified by the document itself
155168
* This will update the document on the server
156169
* This will throw if the document cannot be updated
157170
*
158171
* @throws Exception
159-
* @param mixed $collectionId - collection id as string or number
160172
* @param Document $document - document to be updated
161173
* @param bool $policy - update policy to be used in case of conflict
162174
* @return bool - always true, will throw if there is an error
163175
*/
164-
public function update($collectionId, Document $document, $policy = NULL) {
176+
public function update(Document $document, $policy = NULL) {
165177
$collectionId = $this->getCollectionId($document);
166178
$documentId = $this->getDocumentId($document);
167179

180+
return $this->updateById($collectionId, $documentId, $document, $policy);
181+
}
182+
183+
/**
184+
* Update an existing document in a collection, identified by collection id and document id
185+
* This will update the document on the server
186+
* This will throw if the document cannot be updated
187+
*
188+
* @throws Exception
189+
* @param mixed $collectionId - collection id as string or number
190+
* @param mixed $documentId - document id as string or number
191+
* @param Document $document - document to be updated
192+
* @param bool $policy - update policy to be used in case of conflict
193+
* @return bool - always true, will throw if there is an error
194+
*/
195+
public function updateById($collectionId, $documentId, Document $document, $policy = NULL) {
168196
if ($policy === NULL) {
169197
$policy = $this->getConnection()->getOption(ConnectionOptions::OPTION_UPDATE_POLICY);
170198
}
@@ -180,16 +208,28 @@ public function update($collectionId, Document $document, $policy = NULL) {
180208
}
181209

182210
/**
183-
* Delete a document from a collection
211+
* Delete a document from a collection, identified by the document itself
184212
*
185213
* @throws Exception
186-
* @param mixed $collectionId - collection id as string or number
187-
* @param mixed $document - document id OR document to be updated
214+
* @param Document $document - document to be updated
188215
* @return bool - always true, will throw if there is an error
189216
*/
190-
public function delete($collectionId, $document) {
191-
$documentId = $this->getDocumentId($document);
217+
public function delete(Document $document) {
218+
$collectionId = $this->getCollectionId($document);
219+
$documentId = $this->getDocumentId($document);
192220

221+
return $this->deleteById($collectionId, $documentId);
222+
}
223+
224+
/**
225+
* Delete a document from a collection, identified by the collection id and document id
226+
*
227+
* @throws Exception
228+
* @param mixed $collectionId - collection id as string or number
229+
* @param mixed $documentId - document id as string or number
230+
* @return bool - always true, will throw if there is an error
231+
*/
232+
public function deleteById($collectionId, $documentId) {
193233
$result = $this->getConnection()->delete(UrlHelper::buildUrl(Urls::URL_DOCUMENT, $collectionId, $documentId));
194234

195235
return true;

0 commit comments

Comments
 (0)