/** * Return an array of Document from a Couchbase view. * * @see Couchbase::view * * @SuppressWarnings(PHPMD.StaticAccess) */ public function view($document, $view = '', $options = array(), $returnErrors = false) { $cbResults = $this->connection->view($document, $view, $options, $returnErrors); if (0 == $cbResults['total_rows']) { return array(); } // Convert couchbase raw results to Document collection $func = function ($rawResult) { return new Document($rawResult['id'], $rawResult['value']); }; return array_map($func, $cbResults['rows']); }
<?php echo "--------------------------------------------------------------------------\n"; echo "\tCouchbase Views\n"; echo "--------------------------------------------------------------------------\n"; // Create the view as documented here: // http://www.couchbase.com/docs/couchbase-sdk-php-1.1/tutorial-preparations-cb-server.html $cb = new Couchbase("127.0.0.1:8091", "", "", "beer-sample"); echo "\n--------------------------------------------------------------------------\n"; $views = $cb->getDesignDoc('brewery'); var_export(json_decode($views)); echo "\n--------------------------------------------------------------------------\n"; echo "Breweries (by_name)\n"; $results = $cb->view("brewery", "by_name", array('limit' => 10)); foreach ($results['rows'] as $row) { $doc = $cb->get($row['id']); if ($doc) { $doc = json_decode($doc, true); echo "\nName : "; echo $doc["name"]; } } echo "\n--------------------------------------------------------------------------\n"; echo "Breweries (by_name), and output country\n"; $results = $cb->view("brewery", "by_name", array('limit' => 10)); foreach ($results['rows'] as $row) { $doc = $cb->get($row['id']); if ($doc) { $doc = json_decode($doc, true); echo "\nname : "; echo $doc["name"];