Sample Code

The PHP SDK supports creation and handling of extended attributes.

Subdocument Operations and Extended Attributes

A high-level summary of extended attributes can be found in Extended Attributes. Extended attributes are handled by means of extensions to the Subdocument API.

PHP Extended Attributes Example

The following code demonstrates how extended attributes can be used. It assumes that Couchbase Server is established on localhost; that the Full Administrator username and password are Administrator and password respectively; and that the travel-sample bucket is installed. For information on installing the travel-sample bucket, see Sample Buckets.


$cluster = new \Couchbase\Cluster('couchbase://');
$cluster->authenticateAs('Administrator', 'password');

$bucket = $cluster->openBucket('travel-sample');

// Add key-value pairs to hotel_10138, representing traveller-Ids and associated discount percentages
    ->upsert('discounts.jsmith123', '20', ['xattr' => true, 'createPath' => true])
    ->upsert('discounts.pjones356', '30', ['xattr' => true, 'createPath' => true])
    // The following lines, "insert" and "remove", simply demonstrate insertion and
    // removal of the same path and value
    ->insert('discounts.jbrown789', '25', ['xattr' => true, 'createPath' => true])
    ->remove('discounts.jbrown789', ['xattr' => true])

// Add key - value pairs to hotel_10142, again representing traveller - Ids and associated discount percentages
    ->upsert('discounts.jsmith123', '15', ['xattr' => true, 'createPath' => true])
    ->upsert('discounts.pjones356', '10', ['xattr' => true, 'createPath' => true])

// Create a user and assign roles. This user will search for their available discounts.
$userSettings = new \Couchbase\UserSettings();
    ->role('data_reader', 'travel-sample')
    ->role('query_select', 'travel-sample');
$cluster->manager()->upsertUser('cbtestuser', $userSettings);

// reconnect using new user
$cluster = new \Couchbase\Cluster('couchbase://');
$cluster->authenticateAs('jsmith123', 'jsmith123pwd');

$bucket = $cluster->openBucket('travel-sample');

// Perform a N1QL Query to return document IDs from the bucket. These IDs will be
// used to reference each document in turn, and check for extended attributes
// corresponding to discounts.
$query = \Couchbase\N1qlQuery::fromString('SELECT id, meta(`travel-sample`).id AS docID FROM `travel-sample`');
$result = $bucket->query($query);
$results = '';

foreach ($result->rows as $row) {
    // get row document ID
    $docID = $row->docID;

    // Determine whether a hotel-discount has been applied to this user.
    $res = $bucket->lookupIn($docID)
         ->exists('discounts.jsmith123', ['xattr' => true])
    if ($res->value[0]['code'] == COUCHBASE_SUCCESS) {
        // If so, get the discount-percentage.
        $res = $bucket->lookupIn($docID)
             ->get('discounts.jsmith123', ['xattr' => true])
        $discount = $res->value[0]['value'];

        // If the percentage - value is greater than 15, include the document in the
        // results to be returned.
        $results = sprintf("%s\n%s - %s", $results, $discount, $docID);
printf("Results returned are: %s\n", $results);

Virtual Extended Attributes Example

Using the Sub-Document API, Virtual XATTR can be used to fetch metadata about a document, via the $document virtual XATTR. A common use case is discovering documentation expiration metadata, or TTL:


$cluster = new \Couchbase\Cluster('couchbase://localhost');
$cluster->authenticateAs('Administrator', 'password');

$bucket = $cluster->openBucket('travel-sample');

$key = 'airline_17628';

$bucket->touch($key, 300);

$frag = $bucket->lookupIn($key)
    ->get('$document.exptime', ['xattr' => true])
$ttl = $frag->value[0]['value'];
printf("Expiry value is %s\n", date('c', $ttl));

// Reset the expiry so that the document does not expire
$bucket->touch($key, 0);

$frag = $bucket->lookupIn($key)
    ->get('$document.exptime', ['xattr' => true])
    ->get('$document.value_bytes', ['xattr' => true])

$ttl = $frag->value[0]['value'];
$size = $frag->value[1]['value'];
$callsign = $frag->value[2]['value'];
printf("Expiry value is %s\n", $ttl);
printf("Size is %s bytes\n", $size);
printf("Callsign is %s\n", $callsign);