public static function createDocument($values = array()) { $doc = new couchDocument(self::client()); foreach ($values as $key => $value) { $doc->set($key, $value); } return $doc; }
public function delete() { try { $doc = couchDocument::getInstance(self::$client, $this->properties['_id']); self::$client->deleteDoc($doc); } catch (Exception $ex) { throw new Exception('Document doesn\'t exist'); } }
public function testViewGroup() { $this->_makeView(); $docs = array(array("_id" => "one", "type" => "test", "param" => 1), array("_id" => "two", "type" => "test2", "param" => 2), array("_id" => "three", "type" => "test", "param" => 2), array("_id" => "four", "type" => "test3", "param" => 1), array("_id" => "five", "type" => "test2", "param" => 1)); $this->client->storeDocs($docs); $infos = $this->client->getDatabaseInfos(); $this->assertEquals($infos->doc_count, 6); $doc = couchDocument::getInstance($this->aclient, "_design/test"); $views = $doc->views; $views->multigroup = new stdClass(); $views->multigroup->map = "function (doc) {\n\t\t\tif ( doc.type && doc.param ) {\n\t\t\t\temit( [doc.type, doc.param], 1);\n\t\t\t}\n\t\t}"; $views->multigroup->reduce = "function(keys,values) {\n\t\t\treturn sum(values);\n\t\t}"; $doc->views = $views; $test = $this->client->group(true)->getView("test", "multigroup"); $this->assertInternalType("object", $test); $this->assertObjectHasAttribute("rows", $test); $this->assertInternalType("array", $test->rows); $this->assertEquals(count($test->rows), 5); $test = $this->client->group(true)->group_level(1)->getView("test", "multigroup"); $this->assertInternalType("object", $test); $this->assertObjectHasAttribute("rows", $test); $this->assertInternalType("array", $test->rows); $this->assertEquals(count($test->rows), 3); }
/** * returns couchDB view results as couchDocuments objects * * - for string view keys, the object is found on "view key" index * ex : view returns * <code>[ "client" : null , "client2" : null ]</code> * is translated to : * array ( 'client' => array(couchDocument) , 'client2' => array(couchDocument) ) * * - for array view keys, the object key in the result array is the last key of the view * ex : view returns * <code>[ [ "#44556643", "client" ] : null , [ "#65745767566","client2" : null ]</code> * is translated to : * array ( 'client' => array(couchDocument) , 'client2' => array(couchDocument) ) * * @param stdClass couchDb view resultset * @return array array of couchDocument objects */ public function resultsToCouchDocuments($results) { if (!$results->rows or !is_array($results->rows)) { return FALSE; } $back = array(); foreach ($results->rows as $row) { // should have $row->key & $row->doc if (!$row->key or !$row->doc) { return false; } // create couchDocument $cd = new couchDocument($this); $cd->loadFromObject($row->doc); // set key name if (is_string($row->key)) { $key = $row->key; } elseif (is_array($row->key)) { if (!is_array(end($row->key)) && !is_object(end($row->key))) { $key = end($row->key); } else { continue; } } // set value in result array if (isset($back[$key])) { if (is_array($back[$key])) { $back[$key][] = $cd; } else { $back[$key] = array($back[$key], $cd); } } else { $back[$key] = $cd; } } return $back; }
/** * load a document in a couchDocument object and return it * * @static * @param couchClient $client couchClient instance * @param string $id id of the document to load * @return couchDocument couch document loaded with data of document $id */ public static function getInstance(couchClient $client, $id) { $back = new couchDocument($client); return $back->load($id); }
public function testRevs() { $cd = new couchDocument($this->client); $cd->set(array('_id' => 'somedoc')); $cd->property1 = "one"; $cd->property2 = "two"; $doc = $this->client->revs()->revs_info()->getDoc("somedoc"); $this->assertObjectHasAttribute("_revisions", $doc); $this->assertObjectHasAttribute("ids", $doc->_revisions); $this->assertEquals(count($doc->_revisions->ids), 3); $this->assertObjectHasAttribute("_revs_info", $doc); $this->assertEquals(count($doc->_revs_info), 3); }