PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Querying a collection

To find documents in the collection that meet some criteria, use the query()method. This method accepts either a string which is an adapter-dependent query and is passed as-is to the concrete adapter, or a structured query object instance of Zend_Cloud_DocumentService_Query. The return is a Zend_Cloud_DocumentService_DocumentSet, containing instances of Zend_Cloud_DocumentService_Document that satisfy the query. The DocumentSet object is iterable and countable.

Example 82. Querying a collection using a string query

<?php
$docs 
$documents->query(
    
"collectionName",
    
"RowKey eq 'rowkey2' or RowKey eq 'rowkey2'"
);

foreach (
$docs as $doc) {
    
$id $doc->getId();
    echo 
"Found document with ID: "
        
var_export($id1)
        . 
"\n";
}

If using a structured query object, typically, you will retrieve it using the select() method. This ensures that the query object is specific to your adapter, which will ensure that it is assembled into a syntax your adapter understands.

Example 83. Querying a collection with structured query

<?php
$query 
$service->select();
$query->from('collectionName')
      ->
where('year > ?', array(1945))
      ->
limit(3);
$docs $documents->query('collectionName'$query);

foreach (
$docs as $doc) {
    
$id $doc->getId();
    echo 
"Found document with ID: "
        
var_export($id1)
        . 
"\n";
}

Zend_Cloud_DocumentService_Query classes do not limit which query clauses can be used, but the clause must be supported by the underlying concrete adapter. Currently supported clauses include:

  • select() - defines which fields are returned in the result.

    Note

    Windows Azure ignores this clause's argument and always returns the whole document.

  • from() - defines the collection name used in the query.

  • where() - defines the conditions of the query. It accepts three parameters: condition, array of arguments to replace "?" fields in the condition, and a conjunction argument which should be "and" or "or", and which will be used to join this condition with previous conditions. Multiple where() clasues may be specified.

  • whereId() - defines the condition by document ID (key). The document matching must have the same key. The method accepts one argument - the required ID (key).

  • limit() - limits the returned data to specified number of documents.

  • order() - sorts the returned data by specified field. Accepts two arguments - first is the field name and second is 'asc' or 'desc' specifying the sort direction.

    Note

    This clause is not currently supported by Windows Azure.

Zend Framework