Zend_Dojo-Data.xml 15 KB

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