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

Advanced Use Cases

Besides acting as a serializable data container, Zend_Dojo_Data also provides the ability to manipulate and traverse the data in a variety of ways.

Zend_Dojo_Data implements the interfaces ArrayAccess, Iterator, and Countable. You can therefore use the data collection almost as if it were an array.

All items are referenced by the identifier field. Since identifiers must be unique, you can use the values of this field to pull individual records. There are two ways to do this: with the getItem() method, or via array notation.

<?php
// Using getItem():
$item $data->getItem('foo');

// Or use array notation:
$item $data['foo'];

If you know the identifier, you can use it to retrieve an item, update it, delete it, create it, or test for it:

<?php
// Update or create an item:
$data['foo'] = array('title' => 'Foo''email' => 'foo@foo.com');

// Delete an item:
unset($data['foo']);

// Test for an item:
if (isset($data[foo])) {
}

You can loop over all items as well. Internally, all items are stored as arrays.

<?php
foreach ($data as $item) {
    echo 
$item['title'] . ': ' $item['description'] . "\n";
}

Or even count to see how many items you have:

<?php
echo count($data), " items found!";

Finally, as the class implements __toString(), you can also cast it to JSON simply by echoing it or casting to string:

<?php
echo $data// echo as JSON string

$json = (string) $data// cast to string == cast to JSON

Available Methods

Besides the methods necessary for implementing the interfaces listed above, the following methods are available.

  • setItems($items): set multiple items at once, overwriting any items that were previously set in the object. $items should be an array or a Traversable object.

  • setItem($item, $id = null): set an individual item, optionally passing an explicit identifier. Overwrites the item if it is already in the collection. Valid items include associative arrays, objects implementing toArray(), or any object with public properties.

  • addItem($item, $id = null): add an individual item, optionally passing an explicit identifier. Will raise an exception if the item already exists in the collection. Valid items include associative arrays, objects implementing toArray(), or any object with public properties.

  • addItems($items): add multiple items at once, appending them to any current items. Will raise an exception if any of the new items have an identifier matching an identifier already in the collection. $items should be an array or a Traversable object.

  • getItems(): retrieve all items as an array of arrays.

  • hasItem($id): determine whether an item with the given identifier exists in the collection.

  • getItem($id): retrieve an item with the given identifier from the collection; the item returned will be an associative array. If no item matches, a NULL value is returned.

  • removeItem($id): remove an item with the given identifier from the collection.

  • clearItems(): remove all items from the collection.

  • setIdentifier($identifier): set the name of the field that represents the unique identifier for each item in the collection.

  • getIdentifier(): retrieve the name of the identifier field.

  • setLabel($label): set the name of a field to be used as a display label for an item.

  • getLabel(): retrieve the label field name.

  • toArray(): cast the object to an array. At a minimum, the array will contain the keys 'identifier', 'items', and 'label' if a label field has been set in the object.

  • toJson(): cast the object to a JSON representation.

Zend Framework