| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.dojo.data">
- <title>Zend_Dojo_Data: dojo.data Envelopes</title>
- <para>
- Dojo provides data abstractions for data-enabled widgets via its
- <command>dojo.data</command> component. This component provides the ability to attach a
- data store, provide some metadata regarding the identity field and
- optionally a label field, and an <acronym>API</acronym> for querying, sorting, and
- retrieving records and sets of records from the datastore.
- </para>
- <para>
- <command>dojo.data</command> is often used with XmlHttpRequest to pull dynamic data from
- the server. The primary mechanism for this is to extend the
- QueryReadStore to point at a <acronym>URL</acronym> and specify the query information. The
- server side then returns data in the following <acronym>JSON</acronym> format:
- </para>
- <programlisting language="javascript"><![CDATA[
- {
- identifier: '<name>',
- <label: '<label>',>
- items: [
- { name: '...', label: '...', someKey: '...' },
- ...
- ]
- }
- ]]></programlisting>
- <para>
- <classname>Zend_Dojo_Data</classname> provides a simple interface for building
- such structures programmatically, interacting with them, and serializing
- them to an array or <acronym>JSON</acronym>.
- </para>
- <sect2 id="zend.dojo.data.usage">
- <title>Zend_Dojo_Data Usage</title>
- <para>
- At its simplest, <command>dojo.data</command> 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:
- </para>
- <example id="zend.dojo.data.usage.constructor">
- <title>Zend_Dojo_Data initialization via constructor</title>
- <programlisting language="php"><![CDATA[
- $data = new Zend_Dojo_Data('id', $items);
- ]]></programlisting>
- </example>
- <example id="zend.dojo.data.usage.mutators">
- <title>Zend_Dojo_Data initialization via mutators</title>
- <programlisting language="php"><![CDATA[
- $data = new Zend_Dojo_Data();
- $data->setIdentifier('id')
- ->addItems($items);
- ]]></programlisting>
- </example>
- <para>
- You can also add a single item at a time, or append items, using
- <methodname>addItem()</methodname> and <methodname>addItems()</methodname>.
- </para>
- <example id="zend.dojo.data.usage.append">
- <title>Appending data to Zend_Dojo_Data</title>
- <programlisting language="php"><![CDATA[
- $data = new Zend_Dojo_Data($identifier, $items);
- $data->addItem($someItem);
- $data->addItems($someMoreItems);
- ]]></programlisting>
- </example>
- <note>
- <title>Always use an identifier!</title>
- <para>
- Every <command>dojo.data</command> datastore requires that the identifier column
- be provided as metadata, including <classname>Zend_Dojo_Data</classname>. In fact,
- if you attempt to add items without an identifier, it will raise an exception.
- </para>
- </note>
- <para>
- Individual items may be one of the following:
- </para>
- <itemizedlist>
- <listitem><para>Associative arrays</para></listitem>
- <listitem>
- <para>Objects implementing a <methodname>toArray()</methodname> method</para>
- </listitem>
- <listitem>
- <para>
- Any other objects (will serialize via
- <methodname>get_object_vars()</methodname>)
- </para>
- </listitem>
- </itemizedlist>
- <para>
- You can attach collections of the above items via
- <methodname>addItems()</methodname> or <methodname>setItems()</methodname> (overwrites
- all previously set items); when doing so, you may pass a single argument:
- </para>
- <itemizedlist>
- <listitem><para>Arrays</para></listitem>
- <listitem>
- <para>
- Objects implementing the <classname>Traversable</classname> interface
- ,which includes the interfaces <classname>Iterator</classname> and
- <classname>ArrayAccess</classname>.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- If you want to specify a field that will act as a label for the
- item, call <methodname>setLabel()</methodname>:
- </para>
- <example id="zend.dojo.data.usage.label">
- <title>Specifying a label field in Zend_Dojo_Data</title>
- <programlisting language="php"><![CDATA[
- $data->setLabel('name');
- ]]></programlisting>
- </example>
- <para>
- Finally, you can also load a <classname>Zend_Dojo_Data</classname> item from a
- <command>dojo.data</command> <acronym>JSON</acronym> array, using the
- <methodname>fromJson()</methodname> method.
- </para>
- <example id="zend.dojo.data.usage.populate">
- <title>Populating Zend_Dojo_Data from JSON</title>
- <programlisting language="php"><![CDATA[
- $data->fromJson($json);
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.dojo.data.metadata">
- <title>Adding metadata to your containers</title>
- <para>
- Some Dojo components require additional metadata along with
- the <command>dojo.data</command> payload. As an example,
- <command>dojox.grid.Grid</command> can pull data dynamically from a
- <command>dojox.data.QueryReadStore</command>. For pagination to work
- correctly, each return payload should contain a <property>numRows</property>
- key with the total number of rows that could be returned by the
- query. With this data, the grid knows when to continue making small
- requests to the server for subsets of data and when to stop
- making more requests (i.e., it has reached the last page of data).
- This technique is useful for serving large sets of data in your
- grids without loading the entire set at once.
- </para>
- <para>
- <classname>Zend_Dojo_Data</classname> allows assigning metadata properties as
- to the object. The following illustrates usage:
- </para>
- <programlisting language="php"><![CDATA[
- // Set the "numRows" to 100
- $data->setMetadata('numRows', 100);
- // Set several items at once:
- $data->setMetadata(array(
- 'numRows' => 100,
- 'sort' => 'name',
- ));
- // Inspect a single metadata value:
- $numRows = $data->getMetadata('numRows');
- // Inspect all metadata:
- $metadata = $data->getMetadata();
- // Remove a metadata item:
- $data->clearMetadata('numRows');
- // Remove all metadata:
- $data->clearMetadata();
- ]]></programlisting>
- </sect2>
- <sect2 id="zend.dojo.data.advanced">
- <title>Advanced Use Cases</title>
- <para>
- Besides acting as a serializable data container,
- <classname>Zend_Dojo_Data</classname> also provides the ability to manipulate
- and traverse the data in a variety of ways.
- </para>
- <para>
- <classname>Zend_Dojo_Data</classname> implements the interfaces
- <classname>ArrayAccess</classname>, <classname>Iterator</classname>, and
- <classname>Countable</classname>. You can therefore use the data
- collection almost as if it were an array.
- </para>
- <para>
- 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
- <methodname>getItem()</methodname> method, or via array notation.
- </para>
- <programlisting language="php"><![CDATA[
- // Using getItem():
- $item = $data->getItem('foo');
- // Or use array notation:
- $item = $data['foo'];
- ]]></programlisting>
- <para>
- If you know the identifier, you can use it to retrieve an item,
- update it, delete it, create it, or test for it:
- </para>
- <programlisting language="php"><![CDATA[
- // 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])) {
- }
- ]]></programlisting>
- <para>
- You can loop over all items as well. Internally, all items are
- stored as arrays.
- </para>
- <programlisting language="php"><![CDATA[
- foreach ($data as $item) {
- echo $item['title'] . ': ' . $item['description'] . "\n";
- }
- ]]></programlisting>
- <para>
- Or even count to see how many items you have:
- </para>
- <programlisting language="php"><![CDATA[
- echo count($data), " items found!";
- ]]></programlisting>
- <para>
- Finally, as the class implements <methodname>__toString()</methodname>, you can
- also cast it to <acronym>JSON</acronym> simply by echoing it or casting to string:
- </para>
- <programlisting language="php"><![CDATA[
- echo $data; // echo as JSON string
- $json = (string) $data; // cast to string == cast to JSON
- ]]></programlisting>
- <sect3 id="zend.dojo.data.advanced.methods">
- <title>Available Methods</title>
- <para>
- Besides the methods necessary for implementing the interfaces
- listed above, the following methods are available.
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <methodname>setItems($items)</methodname>: set multiple items at once,
- overwriting any items that were previously set in the
- object. <varname>$items</varname> should be an array or a
- <classname>Traversable</classname> object.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>setItem($item, $id = null)</methodname>: 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
- <methodname>toArray()</methodname>, or any object with public properties.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>addItem($item, $id = null)</methodname>: 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
- <methodname>toArray()</methodname>, or any object with public properties.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>addItems($items)</methodname>: 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. <varname>$items</varname>
- should be an array or a <classname>Traversable</classname> object.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getItems()</methodname>: retrieve all items as an array of
- arrays.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>hasItem($id)</methodname>: determine whether an item with
- the given identifier exists in the collection.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getItem($id)</methodname>: retrieve an item with the given
- identifier from the collection; the item returned will be an associative
- array. If no item matches, a <constant>NULL</constant> value is returned.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>removeItem($id)</methodname>: remove an item with the given
- identifier from the collection.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>clearItems()</methodname>: remove all items from the collection.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>setIdentifier($identifier)</methodname>: set the name of the
- field that represents the unique identifier for each item in
- the collection.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getIdentifier()</methodname>: retrieve the name of the
- identifier field.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>setLabel($label)</methodname>: set the name of a field
- to be used as a display label for an item.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getLabel()</methodname>: retrieve the label field name.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>toArray()</methodname>: 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.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>toJson()</methodname>: cast the object to a
- <acronym>JSON</acronym> representation.
- </para>
- </listitem>
- </itemizedlist>
- </sect3>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|