/** * Save document as new or modified document * */ public function save() { if (!isset($this->_metadata['_id'])) { // Creating a new document $newDoc = $this->_db->create($this->_data, $this->_url); $this->_metadata['_id'] = $newDoc->getId(); $this->_metadata['_rev'] = $newDoc->getRevision(); $this->_url = $newDoc->getUrl(); } else { // Updating an existing document $this->_db->update($this, $this->_url); } }
protected function _initDb() { $dbConfig = $this->_options['couchdb']; Geves_Model_Object::setDbConfig($dbConfig); if ($dbConfig['validate']) { // Make sure the DB exists try { $db = Sopha_Db::createDb($dbConfig['dbname'], $dbConfig['hostname'], $dbConfig['port']); } catch (Sopha_Db_Exception $ex) { if ($ex->getCode() != 412) { throw $ex; } $db = Geves_Model_Object::getDb(); } // Load design documents and create them $views = (require APPLICATION_PATH . '/configs/couchdb-views.php'); if (!is_array($views)) { throw new ErrorException("Unable to configure database views: \$views is not properly defined"); } foreach ($views as $view => $viewData) { try { $db->create($viewData, array('_design', $view)); } catch (Sopha_Db_Exception $ex) { if ($ex->getCode() == 409) { // document already exists if ($dbConfig['replaceViews']) { $view = $db->retrieve(array('_design', $view)); $view->fromArray($viewData); $db->update($view); } } else { throw $ex; } } } } }
/** * Destroy an existing DB after a test * */ protected function _teardownDb() { list($host, $port, $db) = $this->_getUrlParts(); $db = trim($db, '/'); try { Sopha_Db::deleteDb($db, $host, $port); } catch (Sopha_Db_Exception $e) { if ($e->getCode() != 404) { throw $e; } } }