Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

working version of document DELETE

  • Loading branch information...
commit a1a77dd8f7ad0b265340038321d734aa1917abac 1 parent ff5b071
Arin Sarkissian authored
View
61 Duckk_CouchDB/Duckk/CouchDB.php
@@ -71,7 +71,7 @@ public function getDatabases()
public function createDatabase($database)
{
$status = $this->connection->put(
- Duckk_CouchDB_Util::makeDatabaseURI($database)
+ Duckk_CouchDB_Util::makeDatabaseURI($database, null, 'application/json')
);
// return the unserialized json from couch
@@ -290,16 +290,20 @@ public function getDocumentRevisionInfo($database, $documentId)
*/
public function putDocument($database, Duckk_CouchDB_Document &$doc, $batchOK = false)
{
- if (! $doc->_rev) {
+ if (! $doc->hasRevision()) {
unset($doc->_rev);
}
+ if (! $doc->hasAttachments()) {
+ unset($doc->_attachments);
+ }
+
$uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
. $doc->_id
. (($batchOK) ? '?batch=ok' : '');
$json = json_encode($doc);
- $resp = $this->connection->put($uri, $json);
+ $resp = $this->connection->put($uri, $json, 'application/json');
if (isset($resp->error)) {
require_once 'Duckk/CouchDB/Exception/UpdateConflict.php';
@@ -332,7 +336,7 @@ public function postDocument($database, Duckk_CouchDB_Document &$doc)
{
$uri = Duckk_CouchDB_Util::makeDatabaseURI($database);
$json = json_encode($doc);
- $resp = $this->connection->post($uri, $json);
+ $resp = $this->connection->post($uri, $json, 'application/json');
// TODO: check document ID collission
$doc->_rev = $resp->rev;
@@ -340,6 +344,55 @@ public function postDocument($database, Duckk_CouchDB_Document &$doc)
return $resp;
}
+
+ /**
+ * Delete a document
+ *
+ * You can either pass in a db name with the document id, or the db name with an
+ * instance of Duckk_CouchDB_Document.
+ *
+ * If you dont pass in a Duckk_CouchDB_Document for $documentID then you have to
+ * pass in the $documentRevision as the 3rd param
+ *
+ * @param string $database The name of the DB the document is in
+ * @param string|Duckk_CouchDB_Document $documentID The document's ID or the document itself
+ * @param string $documentRevision The rev of the document to delete
+ *
+ * @return stdClass
+ */
+ public function deleteDocument($database, $documentID, $documentRevision = null)
+ {
+ $uri = null;
+ if ($documentID instanceof Duckk_CouchDB_Document) {
+ $uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
+ . $documentID->_id
+ . "?rev={$documentID->_rev}";
+ } else {
+ $uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
+ . $documentID
+ . "?rev={$documentRevision}";
+ }
+
+ $resp = $this->connection->delete($uri);
+
+ if (isset($resp->error)) {
+ require_once 'Duckk/CouchDB/Exception/UpdateConflict.php';
+ throw new Duckk_CouchDB_Exception_UpdateConflict($uri, $resp);
+ }
+
+ return $resp;
+ }
+
+ public function putAttachment($database, $documentID,
+ $attachmentContentType, $attachmentData,
+ $documentRevision = null)
+ {
+ $uri = Duckk_CouchDB_Util::makeDatabaseURI($database)
+ . $documentID
+ . (($documentRevision) ? "?rev={$documentRevision}" : '');
+
+ $resp = $this->connection->put($uri, $json, $attachmentContentType);
+ }
}
?>
View
12 Duckk_CouchDB/Duckk/CouchDB/Connection.php
@@ -40,9 +40,11 @@ public function get($uri)
return json_decode($json);
}
- public function post($uri, $body = null)
+ public function post($uri, $body = null, $contentType = null)
{
- $this->addHeader('Content-Type', 'application/json');
+ if ($contentType) {
+ $this->addHeader('Content-Type', $contentType);
+ }
$resp = parent::post($uri, $body);
$json = $this->getResponseBody();
@@ -50,9 +52,11 @@ public function post($uri, $body = null)
return json_decode($json);
}
- public function put($uri, $body = null)
+ public function put($uri, $body = null, $contentType = null)
{
- $this->addHeader('Content-Type', 'application/json');
+ if ($contentType) {
+ $this->addHeader('Content-Type', $contentType);
+ }
$resp = parent::put($uri, $body);
$json = $this->getResponseBody();
View
20 Duckk_CouchDB/Duckk/CouchDB/Document.php
@@ -78,6 +78,26 @@ public function addAttachment($name, $data, $contentType, $alreadyEncoded = fals
return $this->_attachments[$name];
}
+
+ /**
+ * See if there's any attachments
+ *
+ * @return bool
+ */
+ public function hasAttachments()
+ {
+ return (! empty($this->_attachments));
+ }
+
+ /**
+ * See if the document has a revision or not
+ *
+ * @return bool
+ */
+ public function hasRevision()
+ {
+ return ((bool)$this->_rev);
+ }
}
?>
View
19 examples/example.php
@@ -9,26 +9,21 @@
require_once('Duckk/CouchDB.php');
require_once('Duckk/CouchDB/Util.php');
-$couchdb = new Duckk_CouchDB();
+$couchdb = new Duckk_CouchDB();
+$randomDoc = "testing_" . substr(md5(microtime(true)), 0, 8);
+echo "------------ example doc put (id = $randomDoc) -------------------\n";
$doc = new Duckk_CouchDB_Document();
-$doc->_id = 'abcdddd' . md5(time());
-$doc->_rev = md5(time() . 'arin');
-$doc->name = 'arin';
+$doc->_id = $randomDoc;
+print_r($couchdb->putDocument('arin', $doc));
-$doc->addAttachmentByPath(EXAMPLE_PATH . '/peanut.jpg');
-print_r($couchdb->postDocument('arin', $doc, true));
-
-print_r($doc);
+echo "------------ delete the example doc ($randomDoc) -------------------\n";
+print_r($couchdb->deleteDocument('arin', $doc->_id));
exit;
-
-
-
echo "------------ get my test document --------------\n";
-
try {
print_r($couchdb->getDocument('arin', 'booya3'));
} catch(Exception $e) {
Please sign in to comment.
Something went wrong with that request. Please try again.