Zend_Dojo-Data.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.dojo.data">
  4. <title>Zend_Dojo_Data: dojo.data Envelopes</title>
  5. <para>
  6. Dojo provides data abstraction for data-enabled widgets via its
  7. dojo.data component. This component provides the ability to attach a
  8. data store, provide some metadata regarding the identity field and
  9. optionally a label field, and an API for querying, sorting, and
  10. retrieving records and sets of records from the datastore.
  11. </para>
  12. <para>
  13. dojo.data is often used with XmlHttpRequest to pull dynamic data from
  14. the server. The primary mechanism for this is to extend the
  15. QueryReadStore to point at a URL and specify the query information. The
  16. server side then returns data in the following JSON format:
  17. </para>
  18. <programlisting language="javascript"><![CDATA[
  19. {
  20. identifier: '<name>',
  21. <label: '<label>',>
  22. items: [
  23. { name: '...', label: '...', someKey: '...' },
  24. ...
  25. ]
  26. }
  27. ]]></programlisting>
  28. <para>
  29. <classname>Zend_Dojo_Data</classname> provides a simple interface for building
  30. such structures programmatically, interacting with them, and serializing
  31. them to an array or JSON.
  32. </para>
  33. <sect2 id="zend.dojo.data.usage">
  34. <title>Zend_Dojo_Data Usage</title>
  35. <para>
  36. At its simplest, dojo.data requires that you provide the name of the
  37. identifier field in each item, and a set of items (data). You
  38. can either pass these in via the constructor, or via mutators:
  39. </para>
  40. <example id="zend.dojo.data.usage.constructor">
  41. <title>Zend_Dojo_Data initialization via constructor</title>
  42. <programlisting language="php"><![CDATA[
  43. $data = new Zend_Dojo_Data('id', $items);
  44. ]]></programlisting>
  45. </example>
  46. <example id="zend.dojo.data.usage.mutators">
  47. <title>Zend_Dojo_Data initialization via mutators</title>
  48. <programlisting language="php"><![CDATA[
  49. $data = new Zend_Dojo_Data();
  50. $data->setIdentifier('id')
  51. ->addItems($items);
  52. ]]></programlisting>
  53. </example>
  54. <para>
  55. You can also add a single item at a time, or append items, using
  56. <code>addItem()</code> and <code>addItems()</code>.
  57. </para>
  58. <example id="zend.dojo.data.usage.append">
  59. <title>Appending data to Zend_Dojo_Data</title>
  60. <programlisting language="php"><![CDATA[
  61. $data = new Zend_Dojo_Data($identifier, $items);
  62. $data->addItem($someItem);
  63. $data->addItems($someMoreItems);
  64. ]]></programlisting>
  65. </example>
  66. <note>
  67. <title>Always use an identifier!</title>
  68. <para>
  69. Every dojo.data data store requires that the identifier column
  70. be provided as metadata, including <classname>Zend_Dojo_Data</classname>. In fact,
  71. if you attempt to add items without an identifier, it will raise an exception.
  72. </para>
  73. </note>
  74. <para>
  75. Individual items may be one of the following:
  76. </para>
  77. <itemizedlist>
  78. <listitem><para>
  79. Associative arrays
  80. </para></listitem>
  81. <listitem><para>
  82. Objects implementing a <code>toArray()</code> method
  83. </para></listitem>
  84. <listitem><para>
  85. Any other objects (will serialize via get_object_vars())
  86. </para></listitem>
  87. </itemizedlist>
  88. <para>
  89. You can attach collections of the above items via
  90. <code>addItems()</code> or <code>setItems()</code> (overwrites all
  91. previously set items); when doing so, you may pass a single
  92. argument:
  93. </para>
  94. <itemizedlist>
  95. <listitem><para>
  96. Arrays
  97. </para></listitem>
  98. <listitem><para>
  99. Objects implementing the <code>Traversable</code> interface
  100. ,which includes the interfaces <code>Iterator</code> and
  101. <code>ArrayAccess</code>.
  102. </para></listitem>
  103. </itemizedlist>
  104. <para>
  105. If you want to specify a field that will act as a label for the
  106. item, call <code>setLabel()</code>:
  107. </para>
  108. <example id="zend.dojo.data.usage.label">
  109. <title>Specifying a label field in Zend_Dojo_Data</title>
  110. <programlisting language="php"><![CDATA[
  111. $data->setLabel('name');
  112. ]]></programlisting>
  113. </example>
  114. <para>
  115. Finally, you can also load a <classname>Zend_Dojo_Data</classname> item from a
  116. dojo.data JSON array, using the <code>fromJson()</code> method.
  117. </para>
  118. <example id="zend.dojo.data.usage.populate">
  119. <title>Populating Zend_Dojo_Data from JSON</title>
  120. <programlisting language="php"><![CDATA[
  121. $data->fromJson($json);
  122. ]]></programlisting>
  123. </example>
  124. </sect2>
  125. <sect2 id="zend.dojo.data.metadata">
  126. <title>Adding metadata to your containers</title>
  127. <para>
  128. Some Dojo components require additional metadata along with
  129. the dojo.data payload. As an example, <code>dojox.grid.Grid</code>
  130. can pull data dynamically from a
  131. <code>dojox.data.QueryReadStore</code>. For pagination to work
  132. correctly, each return payload should contain a <code>numRows</code>
  133. key with the total number of rows that could be returned by the
  134. query. With this data, the grid knows when to continue making small
  135. requests to the server for subsets of data and when to stop
  136. making more requests (i.e., it has reached the last page of data).
  137. This technique is useful for serving large sets of data in your
  138. grids without loading the entire set at once.
  139. </para>
  140. <para>
  141. <classname>Zend_Dojo_Data</classname> allows assigning metadata properties as
  142. to the object. The following illustrates usage:
  143. </para>
  144. <programlisting language="php"><![CDATA[
  145. // Set the "numRows" to 100
  146. $data->setMetadata('numRows', 100);
  147. // Set several items at once:
  148. $data->setMetadata(array(
  149. 'numRows' => 100,
  150. 'sort' => 'name',
  151. ));
  152. // Inspect a single metadata value:
  153. $numRows = $data->getMetadata('numRows');
  154. // Inspect all metadata:
  155. $metadata = $data->getMetadata();
  156. // Remove a metadata item:
  157. $data->clearMetadata('numRows');
  158. // Remove all metadata:
  159. $data->clearMetadata();
  160. ]]></programlisting>
  161. </sect2>
  162. <sect2 id="zend.dojo.data.advanced">
  163. <title>Advanced Use Cases</title>
  164. <para>
  165. Besides acting as a serializable data container,
  166. <classname>Zend_Dojo_Data</classname> also provides the ability to manipulate
  167. and traverse the data in a variety of ways.
  168. </para>
  169. <para>
  170. <classname>Zend_Dojo_Data</classname> implements the interfaces
  171. <code>ArrayAccess</code>, <code>Iterator</code>, and
  172. <code>Countable</code>. You can therefore use the data
  173. collection almost as if it were an array.
  174. </para>
  175. <para>
  176. All items are referenced by the identifier field. Since identifiers
  177. must be unique, you can use the values of this field to pull
  178. individual records. There are two ways to do this: with the
  179. <code>getItem()</code> method, or via array notation.
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. // Using getItem():
  183. $item = $data->getItem('foo');
  184. // Or use array notation:
  185. $item = $data['foo'];
  186. ]]></programlisting>
  187. <para>
  188. If you know the identifier, you can use it to retrieve an item,
  189. update it, delete it, create it, or test for it:
  190. </para>
  191. <programlisting language="php"><![CDATA[
  192. // Update or create an item:
  193. $data['foo'] = array('title' => 'Foo', 'email' => 'foo@foo.com');
  194. // Delete an item:
  195. unset($data['foo']);
  196. // Test for an item:
  197. if (isset($data[foo])) {
  198. }
  199. ]]></programlisting>
  200. <para>
  201. You can loop over all items as well. Internally, all items are
  202. stored as arrays.
  203. </para>
  204. <programlisting language="php"><![CDATA[
  205. foreach ($data as $item) {
  206. echo $item['title'] . ': ' . $item['description'] . "\n";
  207. }
  208. ]]></programlisting>
  209. <para>
  210. Or even count to see how many items you have:
  211. </para>
  212. <programlisting language="php"><![CDATA[
  213. echo count($data), " items found!";
  214. ]]></programlisting>
  215. <para>
  216. Finally, as the class implements <code>__toString()</code>, you can
  217. also cast it to JSON simply by echoing it or casting to string:
  218. </para>
  219. <programlisting language="php"><![CDATA[
  220. echo $data; // echo as JSON string
  221. $json = (string) $data; // cast to string == cast to JSON
  222. ]]></programlisting>
  223. <sect3 id="zend.dojo.data.advanced.methods">
  224. <title>Available Methods</title>
  225. <para>
  226. Besides the methods necessary for implementing the interfaces
  227. listed above, the following methods are available.
  228. </para>
  229. <itemizedlist>
  230. <listitem><para>
  231. <code>setItems($items)</code>: set multiple items at once,
  232. overwriting any items that were previously set in the
  233. object. <code>$items</code> should be an array or a
  234. <code>Traversable</code> object.
  235. </para></listitem>
  236. <listitem><para>
  237. <code>setItem($item, $id = null)</code>: set an individual
  238. item, optionally passing an explicit identifier. Overwrites
  239. the item if it is already in the collection. Valid items
  240. include associative arrays, objects implementing
  241. <code>toArray()</code>, or any object with public
  242. properties.
  243. </para></listitem>
  244. <listitem><para>
  245. <code>addItem($item, $id = null)</code>: add an individual
  246. item, optionally passing an explicit identifier. Will raise
  247. an exception if the item already exists in the collection.
  248. Valid items include associative arrays, objects implementing
  249. <code>toArray()</code>, or any object with public
  250. properties.
  251. </para></listitem>
  252. <listitem><para>
  253. <code>addItems($items)</code>: add multiple items at once,
  254. appending them to any current items. Will raise an exception
  255. if any of the new items have an identifier matching an
  256. identifier already in the collection. <code>$items</code>
  257. should be an array or a <code>Traversable</code> object.
  258. </para></listitem>
  259. <listitem><para>
  260. <code>getItems()</code>: retrieve all items as an array of
  261. arrays.
  262. </para></listitem>
  263. <listitem><para>
  264. <code>hasItem($id)</code>: determine whether an item with
  265. the given identifier exists in the collection.
  266. </para></listitem>
  267. <listitem><para>
  268. <code>getItem($id)</code>: retrieve an item with the given
  269. identifier from the collection; the item returned will be an
  270. associative array. If no item matches, a null value is
  271. returned.
  272. </para></listitem>
  273. <listitem><para>
  274. <code>removeItem($id)</code>: remove an item with the given
  275. identifier from the collection.
  276. </para></listitem>
  277. <listitem><para>
  278. <code>clearItems()</code>: remove all items from the
  279. collection.
  280. </para></listitem>
  281. <listitem><para>
  282. <code>setIdentifier($identifier)</code>: set the name of the
  283. field that represents the unique identifier for each item in
  284. the collection.
  285. </para></listitem>
  286. <listitem><para>
  287. <code>getIdentifier()</code>: retrieve the name of the
  288. identifier field.
  289. </para></listitem>
  290. <listitem><para>
  291. <code>setLabel($label)</code>: set the name of a field
  292. to be used as a display label for an item.
  293. </para></listitem>
  294. <listitem><para>
  295. <code>getLabel()</code>: retrieve the label field name.
  296. </para></listitem>
  297. <listitem><para>
  298. <code>toArray()</code>: cast the object to an array. At a minimum, the
  299. array will contain the keys 'identifier',
  300. 'items', and 'label' if a label field has been set
  301. in the object.
  302. </para></listitem>
  303. <listitem><para>
  304. <code>toJson()</code>: cast the object to a JSON
  305. representation.
  306. </para></listitem>
  307. </itemizedlist>
  308. </sect3>
  309. </sect2>
  310. </sect1>
  311. <!--
  312. vim:se ts=4 sw=4 et:
  313. -->