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

Zend_Dojo_Data Usage

At its simplest, dojo.data requires that you provide the name of the identifier field in each item, and a set of items (data). You can either pass these in via the constructor, or via mutators:

Example 346. Zend_Dojo_Data initialization via constructor

<?php
$data 
= new Zend_Dojo_Data('id'$items);

Example 347. Zend_Dojo_Data initialization via mutators

<?php
$data 
= new Zend_Dojo_Data();
$data->setIdentifier('id')
     ->
addItems($items);

You can also add a single item at a time, or append items, using addItem() and addItems().

Example 348. Appending data to Zend_Dojo_Data

<?php
$data 
= new Zend_Dojo_Data($identifier$items);
$data->addItem($someItem);

$data->addItems($someMoreItems);

Always use an identifier!

Every dojo.data datastore requires that the identifier column be provided as metadata, including Zend_Dojo_Data. In fact, if you attempt to add items without an identifier, it will raise an exception.

Individual items may be one of the following:

  • Associative arrays

  • Objects implementing a toArray() method

  • Any other objects (will serialize via get_object_vars())

You can attach collections of the above items via addItems() or setItems() (overwrites all previously set items); when doing so, you may pass a single argument:

  • Arrays

  • Objects implementing the Traversable interface ,which includes the interfaces Iterator and ArrayAccess.

If you want to specify a field that will act as a label for the item, call setLabel():

Example 349. Specifying a label field in Zend_Dojo_Data

<?php
$data
->setLabel('name');

Finally, you can also load a Zend_Dojo_Data item from a dojo.data JSON array, using the fromJson() method.

Example 350. Populating Zend_Dojo_Data from JSON

<?php
$data
->fromJson($json);

Zend Framework