2017 © Pedro Peláez
 

library cloudant

CouchDB NoSQL database access in PHP

image

redaigbaria/cloudant

CouchDB NoSQL database access in PHP

  • Monday, November 7, 2016
  • by redaigbaria
  • Repository
  • 2 Watchers
  • 5 Stars
  • 520 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Introduction

PHP On Couch tries to provide an easy way to work with your CouchDB documents with PHP. Some code first :, (*1)

<?PHP
require_once 'couch.php';
require_once 'couchClient.php';
require_once 'couchDocument.php';

// set a new connector to the CouchDB server
$client = new couchClient ('http://my.couch.server.com:5984','my_database');

// document fetching by ID
$doc = $client->getDoc('some_doc_id');
// updating document
$doc->newproperty = array("hello !","world");
try {
   $client->storeDoc($doc);
} catch (Exception $e) {
   echo "Document storage failed : ".$e->getMessage()."<BR>\n";
}

// view fetching, using the view option limit
try {
   $view = $client->limit(100)->getView('orders','by-date');
} catch (Exception $e) {
   echo "something weird happened: ".$e->getMessage()."<BR>\n";
}

//using couch_document class :
$doc = new couchDocument($client);
$doc->set( array('_id'=>'JohnSmith','name'=>'Smith','firstname'=>'John') ); //create a document and store it in the database
echo $doc->name ; // should echo "Smith"
$doc->name = "Brown"; // set document property "name" to "Brown" and store the updated document in the database

Components

This library has four main classes and a custom Exception class., (*2)

couch class

This is the most basic of the three classes, and is responsible for the low level dialog between PHP and the CouchDB server. There should be no need of using it directly., (*3)

couchClient class

This class maps all the actions the application can do on the CouchDB server. Documentation is split in three main topics :, (*4)

database stuff

list databases, create and delete a database, retrieve database informations, test whether a database exists, get uuids, get databases changes, (*5)

document stuff

fetching and storing documents, copy a document, store and delete document attachments, getting all documents, (*6)

view stuff

calling a view with query options : key, startkey, endkey, limit, stale, ..., (*7)

couchDocument class

Easing the manipulation of documents, the couchDocument class uses PHP magic getters and setters., (*8)

couchReplicator class

A dedicated class to manage replications over different instances of CouchDB databases., (*9)

couchAdmin class

A class to manage users and database/users associations, (*10)

Quick-start guide

  1. copy couch.php, couchClient.php and couchDocument.php somewhere on your disk, (*11)

  2. Include those files whenever you need to access CouchDB server :, (*12)

    <?PHP
    require_once "couch.php";
    require_once "couchClient.php";
    require_once "couchDocument.php";

If you need to use replication features, also include the couchReplicator definition :, (*13)

    require_once "couchReplicator.php";
  1. Create a client object. You have to tell it the Data source name_ (dsn) of your CouchDB server, as well as the name of the database you want to work on. The DSN is the URL of your CouchDB server, for example _http://localhost:5984., (*14)

    $client = new couchClient($couchdb_server_dsn, $couchdb_database_name);
  2. Use it !, (*15)

    try {
        $client->createDatabase();
    } catch (Exception $e) {
        echo "Unable to create database : ".$e->getMessage();
    }
    
    $doc = new couchDocument($client);
    $doc->set( array('_id'=>'some_doc_id', 'type'=>'story','title'=>"First story") );
    
    $view = $client->limit(10)->descending(TRUE)->getView('some_design_doc','viewname');

Feedback

Don't hesitate to submit feedback, bugs and feature requests ! My contact address is mickael dot bailly at free dot fr, (*16)

Resources

Database API, (*17)

Document API, (*18)

View API, (*19)

couchDocument API, (*20)

couchReplicator API, (*21)

couchAdmin API, (*22)

The Versions

07/11 2016

dev-master

9999999-dev https://github.com/redaigbaria/cloudant

CouchDB NoSQL database access in PHP

  Sources   Download

The Requires

  • php >=5.1.0

 

by Reda Igbara

nosql couchdb driver apache db lucene couch cloudant