sfCouchPlugin
The sfCouch plugin connects symfony to the CouchDB key-value store.
Installation
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!
$this->enablePlugins('sfCouchPlugin');
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)
Configuration
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:
$newDoc->delete();
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');
...
}
Views
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.
print_r(sfCouchView::query('alldocs')->rows);
TODO
- Implement support for standalone files in CouchDB
- Implement multiple key requests