1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
<?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:
-->
|