Exemple #1
0
// This example file shows you how to create, get and delete a view
try {
    $cb = new Couchbase("localhost", "Administrator", "asdasd");
    $viewname = "myview";
    $designdoc = "mydesign";
    // Create the map function.
    $func = "function (doc, meta) { emit(meta.id, NULL); }";
    // Create document containing the map function
    $ddoc = json_encode('{"views":{"' . $viewname . '":{"map":"' . $func . '"}}}');
    // Create the design document on the server
    $ret = $cb->setDesignDoc($designdoc, json_decode($ddoc));
    if ($ret) {
        print "View successfully created" . PHP_EOL;
    } else {
        print "Failed to create view: " . $cb->getResultMessage() . PHP_EOL;
    }
    // Try to retrieve the desgin document:
    $ddoc = $cb->getDesignDoc($designdoc);
    print "The design document looks like: " . PHP_EOL;
    var_dump($ddoc);
    // Delete the design document:
    $ret = $cb->deleteDesignDoc($designdoc);
    if ($ret) {
        print "View successfully deleted" . PHP_EOL;
    } else {
        print "Failed to delete view: " . $cb->getResultMessage() . PHP_EOL;
    }
} catch (CouchbaseException $exp) {
    print "Failed to create view: " . $exp->getMessage() . PHP_EOL;
    var_dump($exp);
}
<?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"];