In support of the upcoming Couchbase Server 4.5 release and some innovation in the PHP community with PHP 7, we are enhancing our PHP SDK. Today I’d like to announce the third Beta, which brings a couple of new fetures along with internal fixes and improvements. In this blog post we will demonstrate some of them.

Sub-Document API

We already wrote about this API previously, but this time lets show some PHP. We assume you have installed Couchbase 4.5 beta as a minimum and have setup the enhanced sample bucket travel-sample. First lets do some service code, where we instantiate a connection to bucket, and also define a utility function to report on a random hotel.

Okay now we are ready. The Sub-Document API allows you save network traffic while operating on large documents by using fine-grained control, for example lets pull some of the properties from the hotel_10025:

Here and later accessor methods accept path as a first argument.  This is similar to the N1QL path as described in the documentation. If you get this document using the regular API, you will see that is isn’t the smallest one and contains lots of information about the place, and with lookupIn you are transferring only data you are interested in. It become more important when you’d like to amend small bits in the huge document. As an example, let’s add one more person to public_likes, adjust the hotel name and remove one of the ratings.

The code is pretty self-explanatory. If you would like to use optimistic locking, just pass CAS value as a second argument to mutateIn. The complete list of mutation operators is in the API reference Let’s display what we have after the change, but this time we will use the shortcut for lookupIn with multiple get operators:

Index Management

Previously to list, create or remove indexes, it was necessary to manually craft the N1QL statement. In the upcoming PHP SDK 2.2 there will be a new API simplifying this task by exposing handy functions on BucketManager, much like for Couchbase Views. Let’s see what indexes are defined on the travel-sample by default:

It should output something like this:

The following example demonstrates creating indexes on name:

Removing indexes is not a little harder:

Before I close, I’d like to show one small but in some cases important change we’ve got in the beta3 release. The CAS value has always been opaque in Couchbase protocol as well as in SDKs, but in PHP it was even hidden from application developer behind the PHP resource reference, which means that it is hard to pass it around easily (for example to render onto HTML page). In this release we encode the CAS as a string token, so it is still opaque, but much easier to serialize it.

If you are not familiar with what CAS is, you can learn it by example here:

And finally, to install this release, use the following command (make sure you have libcouchbase 2.6.0+ installed):

That’s all for now. We plan one more Beta release befor GA with even more features. If you encounter any issues, please post directly to the Couchbase Communities site at Additionally, bugs can be reported directly through our issues tracker available here:

Stay tuned.


Posted by Sergey Avseyev, SDK Engineer, Couchbase

Sergey Avseyev is a SDK Engineer at Couchbase. Sergey Avseyev is responsible for development of Kafka connector, and underlying library, which implements DCP, Couchbase replication protocol. Also maintaining PHP SDK for Couchbase.

Leave a reply