Skip to content

Instantly share code, notes, and snippets.

@ihortymoshenko
Created February 11, 2014 16:00
Show Gist options
  • Save ihortymoshenko/8937735 to your computer and use it in GitHub Desktop.
Save ihortymoshenko/8937735 to your computer and use it in GitHub Desktop.
Couchbase PHP example
#!/bin/sh
# suppose you are using Ubuntu 12.04 x86_64
sudo apt-get update
sudo apt-get install -y vim
# install Couchbase Server
# http://www.couchbase.com/download#cb-server
wget http://packages.couchbase.com/releases/2.2.0/couchbase-server-enterprise_2.2.0_x86_64.deb
sudo dpkg -i couchbase-server-enterprise_2.2.0_x86_64.deb
# install Couchbase C Client Library
# http://www.couchbase.com/communities/c/getting-started
sudo wget -O/etc/apt/sources.list.d/couchbase.list http://packages.couchbase.com/ubuntu/couchbase-ubuntu1204.list
sudo apt-get update
sudo apt-get install -y --allow-unauthenticated libcouchbase2-libevent libcouchbase-dev
# install Couchbase PHP Client library
# http://www.couchbase.com/communities/php/getting-started
sudo apt-get install -y php5
sudo apt-get install -y php-pear
sudo apt-get install -y build-essential
sudo pecl install couchbase
sudo sh -c 'echo "extension=couchbase.so" >> /etc/php5/cli/php.ini'
<?php
require_once __DIR__ . '/UserManager.php';
$couchbase = new \Couchbase('127.0.0.1:8091', 'username', 'password', 'bucket', true);
$userManager = new \UserManager($couchbase);
// create
$id = $userManager->create(array(
'username' => 'Ivan',
'password' => 'qwerty',
));
// read
$userManager->read($id);
// update
$userManager->update($id, array(
'username' => 'Ivan',
'password' => 'asdfgh',
));
// delete
$userManager->delete($id);
// create view (index)
$designDoc = array(
'language' => 'javascript',
'views' => array(
'username' => array(
'map' => <<<JS
function (doc, meta) {
if (meta.type == "json" && doc.username) {
var userid = meta.id.split("::");
emit(doc.username, parseInt(userid[1]));
}
}
JS
),
),
);
$couchbase->setDesignDoc('userFields', json_encode($designDoc));
// read full index
$result = $couchbase->view('userFields', 'username');
// find by the key
$result = $couchbase->view('userFields', 'username', array('key' => 'Ivan', 'stale' => false));
<?php
class UserManager
{
protected $couchbase;
public function __construct(\Couchbase $couchbase)
{
$this->couchbase = $couchbase;
}
public function create(array $data)
{
try {
$id = $this->makeId();
$this->couchbase->add("users::$id", json_encode($data));
return $id;
} catch (\CouchbaseException $e) {
die('Unable to create user. The error is:' . $e->getMessage());
}
}
public function read($id)
{
try {
return json_decode($this->couchbase->get("users::$id"));
} catch (\CouchbaseException $e) {
die('Unable to read user. The error is:' . $e->getMessage());
}
}
public function update($id, array $data)
{
try {
return $this->couchbase->set("users::$id", json_encode($data));
} catch (\CouchbaseException $e) {
die('Unable to update user. The error is:' . $e->getMessage());
}
}
public function delete($id)
{
try {
return $this->couchbase->delete("users::$id");
} catch (\CouchbaseException $e) {
die('Unable to delete user. The error is:' . $e->getMessage());
}
}
protected function makeId()
{
return $this->couchbase->increment('counter::users', 1, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment