Chapter 5. Storing and Updating Recipes

Now that we’ve looked at the document design and the core storage and retrieval methods, let’s look at some specific code examples using our recipe data.

Creating your document and storing it are straightforward. Depending on your client language you can model the data and use serialization to create the JSON structure.

Initial Storage

Storing your initial document requires creating the document structure (as described above), and then using a set() operation to store the data into the database using an appropriate document ID, such as a UUID.

For example:

$recipe = array('title' => 'Lasagne', 'subtitle' => 'Traditional italian pasta dish', 'servings' => 4);
$cb = new Couchbase("127.0.0.1:8091", "recipes", "", "recipes");
$cb->set(uniqid(),$recipe);

To get the best results out of the Couchbase when it comes to querying and extracting data using views, JSON should be used as the storage format.

Most languages support some form of serialization of an internal object into an external format. Sometimes this is just for the purposes of external storage (for example on disk), but more complex solutions exist for object relational modeling (ORM), where an internal object is translated into RDBMS based storage.

With Couchbase Server an alternative model is available, with internal objects being serializable into the JSON format, which allows integration with the built-in document model, views and indexing.

Within the PHP client library there ...

Get Developing with Couchbase Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.