Zend_Dojo-Data.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 abstractions for data-enabled widgets via its
  7. <command>dojo.data</command> 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 <acronym>API</acronym> for querying, sorting, and
  10. retrieving records and sets of records from the datastore.
  11. </para>
  12. <para>
  13. <command>dojo.data</command> 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 <acronym>URL</acronym> and specify the query information. The
  16. server side then returns data in the following <acronym>JSON</acronym> 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 <acronym>JSON</acronym>.
  32. </para>
  33. <sect2 id="zend.dojo.data.usage">
  34. <title>Zend_Dojo_Data Usage</title>
  35. <para>
  36. At its simplest, <command>dojo.data</command> 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. <methodname>addItem()</methodname> and <methodname>addItems()</methodname>.
  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 <command>dojo.data</command> datastore 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>Associative arrays</para></listitem>
  79. <listitem>
  80. <para>Objects implementing a <methodname>toArray()</methodname> method</para>
  81. </listitem>
  82. <listitem>
  83. <para>
  84. Any other objects (will serialize via
  85. <methodname>get_object_vars()</methodname>)
  86. </para>
  87. </listitem>
  88. </itemizedlist>
  89. <para>
  90. You can attach collections of the above items via
  91. <methodname>addItems()</methodname> or <methodname>setItems()</methodname> (overwrites
  92. all previously set items); when doing so, you may pass a single argument:
  93. </para>
  94. <itemizedlist>
  95. <listitem><para>Arrays</para></listitem>
  96. <listitem>
  97. <para>
  98. Objects implementing the <classname>Traversable</classname> interface
  99. ,which includes the interfaces <classname>Iterator</classname> and
  100. <classname>ArrayAccess</classname>.
  101. </para>
  102. </listitem>
  103. </itemizedlist>
  104. <para>
  105. If you want to specify a field that will act as a label for the
  106. item, call <methodname>setLabel()</methodname>:
  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. <command>dojo.data</command> <acronym>JSON</acronym> array, using the
  117. <methodname>fromJson()</methodname> method.
  118. </para>
  119. <example id="zend.dojo.data.usage.populate">
  120. <title>Populating Zend_Dojo_Data from JSON</title>
  121. <programlisting language="php"><![CDATA[
  122. $data->fromJson($json);
  123. ]]></programlisting>
  124. </example>
  125. </sect2>
  126. <sect2 id="zend.dojo.data.metadata">
  127. <title>Adding metadata to your containers</title>
  128. <para>
  129. Some Dojo components require additional metadata along with
  130. the <command>dojo.data</command> payload. As an example,
  131. <command>dojox.grid.Grid</command> can pull data dynamically from a
  132. <command>dojox.data.QueryReadStore</command>. For pagination to work
  133. correctly, each return payload should contain a <property>numRows</property>
  134. key with the total number of rows that could be returned by the
  135. query. With this data, the grid knows when to continue making small
  136. requests to the server for subsets of data and when to stop
  137. making more requests (i.e., it has reached the last page of data).
  138. This technique is useful for serving large sets of data in your
  139. grids without loading the entire set at once.
  140. </para>
  141. <para>
  142. <classname>Zend_Dojo_Data</classname> allows assigning metadata properties as
  143. to the object. The following illustrates usage:
  144. </para>
  145. <programlisting language="php"><![CDATA[
  146. // Set the "numRows" to 100
  147. $data->setMetadata('numRows', 100);
  148. // Set several items at once:
  149. $data->setMetadata(array(
  150. 'numRows' => 100,
  151. 'sort' => 'name',
  152. ));
  153. // Inspect a single metadata value:
  154. $numRows = $data->getMetadata('numRows');
  155. // Inspect all metadata:
  156. $metadata = $data->getMetadata();
  157. // Remove a metadata item:
  158. $data->clearMetadata('numRows');
  159. // Remove all metadata:
  160. $data->clearMetadata();
  161. ]]></programlisting>
  162. </sect2>
  163. <sect2 id="zend.dojo.data.advanced">
  164. <title>Advanced Use Cases</title>
  165. <para>
  166. Besides acting as a serializable data container,
  167. <classname>Zend_Dojo_Data</classname> also provides the ability to manipulate
  168. and traverse the data in a variety of ways.
  169. </para>
  170. <para>
  171. <classname>Zend_Dojo_Data</classname> implements the interfaces
  172. <classname>ArrayAccess</classname>, <classname>Iterator</classname>, and
  173. <classname>Countable</classname>. You can therefore use the data
  174. collection almost as if it were an array.
  175. </para>
  176. <para>
  177. All items are referenced by the identifier field. Since identifiers
  178. must be unique, you can use the values of this field to pull
  179. individual records. There are two ways to do this: with the
  180. <methodname>getItem()</methodname> method, or via array notation.
  181. </para>
  182. <programlisting language="php"><![CDATA[
  183. // Using getItem():
  184. $item = $data->getItem('foo');
  185. // Or use array notation:
  186. $item = $data['foo'];
  187. ]]></programlisting>
  188. <para>
  189. If you know the identifier, you can use it to retrieve an item,
  190. update it, delete it, create it, or test for it:
  191. </para>
  192. <programlisting language="php"><![CDATA[
  193. // Update or create an item:
  194. $data['foo'] = array('title' => 'Foo', 'email' => 'foo@foo.com');
  195. // Delete an item:
  196. unset($data['foo']);
  197. // Test for an item:
  198. if (isset($data[foo])) {
  199. }
  200. ]]></programlisting>
  201. <para>
  202. You can loop over all items as well. Internally, all items are
  203. stored as arrays.
  204. </para>
  205. <programlisting language="php"><![CDATA[
  206. foreach ($data as $item) {
  207. echo $item['title'] . ': ' . $item['description'] . "\n";
  208. }
  209. ]]></programlisting>
  210. <para>
  211. Or even count to see how many items you have:
  212. </para>
  213. <programlisting language="php"><![CDATA[
  214. echo count($data), " items found!";
  215. ]]></programlisting>
  216. <para>
  217. Finally, as the class implements <methodname>__toString()</methodname>, you can
  218. also cast it to <acronym>JSON</acronym> simply by echoing it or casting to string:
  219. </para>
  220. <programlisting language="php"><![CDATA[
  221. echo $data; // echo as JSON string
  222. $json = (string) $data; // cast to string == cast to JSON
  223. ]]></programlisting>
  224. <sect3 id="zend.dojo.data.advanced.methods">
  225. <title>Available Methods</title>
  226. <para>
  227. Besides the methods necessary for implementing the interfaces
  228. listed above, the following methods are available.
  229. </para>
  230. <itemizedlist>
  231. <listitem>
  232. <para>
  233. <methodname>setItems($items)</methodname>: set multiple items at once,
  234. overwriting any items that were previously set in the
  235. object. <varname>$items</varname> should be an array or a
  236. <classname>Traversable</classname> object.
  237. </para>
  238. </listitem>
  239. <listitem>
  240. <para>
  241. <methodname>setItem($item, $id = null)</methodname>: set an individual
  242. item, optionally passing an explicit identifier. Overwrites
  243. the item if it is already in the collection. Valid items
  244. include associative arrays, objects implementing
  245. <methodname>toArray()</methodname>, or any object with public properties.
  246. </para>
  247. </listitem>
  248. <listitem>
  249. <para>
  250. <methodname>addItem($item, $id = null)</methodname>: add an individual
  251. item, optionally passing an explicit identifier. Will raise
  252. an exception if the item already exists in the collection.
  253. Valid items include associative arrays, objects implementing
  254. <methodname>toArray()</methodname>, or any object with public properties.
  255. </para>
  256. </listitem>
  257. <listitem>
  258. <para>
  259. <methodname>addItems($items)</methodname>: add multiple items at once,
  260. appending them to any current items. Will raise an exception
  261. if any of the new items have an identifier matching an
  262. identifier already in the collection. <varname>$items</varname>
  263. should be an array or a <classname>Traversable</classname> object.
  264. </para>
  265. </listitem>
  266. <listitem>
  267. <para>
  268. <methodname>getItems()</methodname>: retrieve all items as an array of
  269. arrays.
  270. </para>
  271. </listitem>
  272. <listitem>
  273. <para>
  274. <methodname>hasItem($id)</methodname>: determine whether an item with
  275. the given identifier exists in the collection.
  276. </para>
  277. </listitem>
  278. <listitem>
  279. <para>
  280. <methodname>getItem($id)</methodname>: retrieve an item with the given
  281. identifier from the collection; the item returned will be an associative
  282. array. If no item matches, a <constant>NULL</constant> value is returned.
  283. </para>
  284. </listitem>
  285. <listitem>
  286. <para>
  287. <methodname>removeItem($id)</methodname>: remove an item with the given
  288. identifier from the collection.
  289. </para>
  290. </listitem>
  291. <listitem>
  292. <para>
  293. <methodname>clearItems()</methodname>: remove all items from the collection.
  294. </para>
  295. </listitem>
  296. <listitem>
  297. <para>
  298. <methodname>setIdentifier($identifier)</methodname>: set the name of the
  299. field that represents the unique identifier for each item in
  300. the collection.
  301. </para>
  302. </listitem>
  303. <listitem>
  304. <para>
  305. <methodname>getIdentifier()</methodname>: retrieve the name of the
  306. identifier field.
  307. </para>
  308. </listitem>
  309. <listitem>
  310. <para>
  311. <methodname>setLabel($label)</methodname>: set the name of a field
  312. to be used as a display label for an item.
  313. </para>
  314. </listitem>
  315. <listitem>
  316. <para>
  317. <methodname>getLabel()</methodname>: retrieve the label field name.
  318. </para>
  319. </listitem>
  320. <listitem>
  321. <para>
  322. <methodname>toArray()</methodname>: cast the object to an array. At a
  323. minimum, the array will contain the keys 'identifier',
  324. 'items', and 'label' if a label field has been set in the object.
  325. </para>
  326. </listitem>
  327. <listitem>
  328. <para>
  329. <methodname>toJson()</methodname>: cast the object to a
  330. <acronym>JSON</acronym> representation.
  331. </para>
  332. </listitem>
  333. </itemizedlist>
  334. </sect3>
  335. </sect2>
  336. </sect1>
  337. <!--
  338. vim:se ts=4 sw=4 et:
  339. -->