sfCouchPlugin - 0.1.4

CouchDB plugin

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
Show source


The sfCouch plugin connects symfony to the CouchDB key-value store.


To install the plugin for a symfony project, the usual process is to use the symfony command line. You have to add the beta option as long as this plugin not stable.

$ symfony plugin:install -s beta sfCouchPlugin

Alternatively, if you don't have PEAR installed, you can download the latest package attached to this plugin's wiki page and extract it under your project's plugins/ directory. Make sure the plugin is enabled in your project's /config/ProjectConfiguration.class.php if you install it manually!


Clear the cache to enable the autoloading to find the new classes:

$ php symfony cc

Copy the file CouchDB.yml and the folder CouchDB from the plugin's config folder to the project's config-folder (/config)


Edit the /config/CouchDB.yml for your CouchDB installation.

Creating documents

To create a new CouchDB document you can just instanciate a sfCouchDocument, set some key/value pairs and save it.

$newDoc = new sfCouchDocument();
$newDoc->type = 'myDoc';
$newDoc->data = array('Banana', 'Orange');
$docId = $newDoc->save();

You can also create a document with an own id (instead of the autogenerated uuid)

$newDoc = new sfCouchDocument('mySuperId');

If a document with this ID exists, it's fetched from the CouchDB and the method newDoc->isNew() returns false. That's the way to get a single document by ID from the db.

Documents can be deleted too:


Files can be attached to a document with:

$newDoc->attachFile($filePath, $fileName, $mimeType);

Only the first parameter is mandatory. If no fileName is given, the Name is derived from the file itself. If no mimetype is given it'll be 'application/octet-stream'.

Attachments can be loaded from a CouchDB document with:

$tempFilePath = $newDoc->getFile($fileName);

The file will be put in the webservers temp folder and the method returns the path of the temp file.

If you want to have some more specialized documents just create a new model class that inherits sfCouchDocument.

class mySpecialDocument extends sfCouchDocument
  // use this property to define keys that have to be set
  protected $requiredProperties = array('type', 'name');



Introduction to views

To create a view just put a javascript file in the folder /config/CouchDB that is named VIEWNAME_map.js. If you also need a reduce function create another file named named VIEWNAME_reduce.js. There's an example view 'alldocs' in the distribution that returns all documents in the db and a map/reduce view called 'example' that outputs the count of the digits 0-9 in the ids of your documents.

Note: In production env the views don't get updated automatically. If you change a javascript you have to call the task couch:refreshViews.

$ php symfony couch:refreshViews

Then call the view with sfCouchView::query($viewName, [$options]). The first parameter ist the VIEWNAME from the javascript files.

$result = sfCouchView::query('alldocs');

The optional second parameter is an array with the CouchDB query options See CouchDB wiki.

$options = array('reduce' => false, 'include_docs' => true); $result = sfCouchView::query('example', $options);

The result is an sfCouchResponse object that holds the returned arrays. You'll be interested in $result->rows.



  • Implement support for standalone files in CouchDB
  • Implement multiple key requests