Skip to content

Commit 28d1498

Browse files
committed
Support updating documents, and handling errors when we do that
1 parent c0183a7 commit 28d1498

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/PHPCouchDB/Database.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,34 @@ public function __construct(\GuzzleHttp\ClientInterface $client, string $db_name
3232
$this->db_name = $db_name;
3333
}
3434

35+
/**
36+
* Get the name of the database
37+
*/
38+
public function getName() : string
39+
{
40+
return $this->db_name;
41+
}
42+
43+
/**
44+
* If you need to make a request that isn't supported by this library,
45+
* use this method to get the client to use. Aimed at more advanced
46+
* users/requirements
47+
*/
48+
public function getClient() : \GuzzleHttp\ClientInterface
49+
{
50+
return $this->client;
51+
}
52+
53+
/**
54+
* Format object for var_dump(), removing large properties
55+
*/
56+
public function __debugInfo()
57+
{
58+
$result = get_object_vars($this);
59+
unset($result['client']);
60+
return $result;
61+
}
62+
3563
/**
3664
* Fetch all the documents from the database
3765
*

src/PHPCouchDB/Document.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,42 @@ public function __debugInfo()
5353
unset($result['database']);
5454
return $result;
5555
}
56+
57+
/**
58+
* Saves the current state of the document, returning a NEW object to
59+
* represent the new document revision. If the doc is outdated, the update
60+
* fails.
61+
*
62+
* @return Document The updated doc
63+
* @throws \PHPCouchDB\Exception\DocumentConflictException if the update
64+
* fails because we don't have the (correct) revision number
65+
* @throws \PHPCouchDB\Exception\DatabaseExecption if something else goes
66+
* wrong, with the previous exception included
67+
*/
68+
public function update()
69+
{
70+
$endpoint = "/" . $this->database->getName() . "/" . $this->id;
71+
72+
// take a copy and drop out the internal values before sending
73+
$doc = get_object_vars($this);
74+
$doc['_rev'] = $doc['rev'];
75+
unset($doc['client']);
76+
unset($doc['id']);
77+
unset($doc['rev']);
78+
unset($doc['database']);
79+
80+
try {
81+
$response = $this->client->request('PUT', $endpoint, ["json" => $doc]);
82+
// get a brand new version and return it as a brand new object
83+
$newrev = $this->database->getDocById($this->id);
84+
return $newrev;
85+
} catch (\GuzzleHTTP\Exception\ClientException $e) {
86+
// is it a conflict? Or something else?
87+
if($e->getResponse()->getStatusCode() == 409) {
88+
throw new Exception\DocumentConflictException('Conflict. Outdated or missing revision information');
89+
} else {
90+
throw new Exception\DatabseException('The update failed', 0, $e);
91+
}
92+
}
93+
}
5694
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PHPCouchDB\Exception;
4+
5+
class DocumentConflictException extends \Exception
6+
{
7+
}
8+

0 commit comments

Comments
 (0)