Zend_Cloud_DocumentService.xml 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.cloud.documentservice">
  4. <title>Document Service Introduction</title>
  5. <para>
  6. <classname>Zend_Cloud_DocumentService</classname> abstracts the interfaces to all major
  7. document databases - both in the cloud and locally deployed - so developers can access their
  8. common functionality through one API. In other words, an application can make use of these
  9. databases and services with no concern over how the application will be deployed. The data
  10. source can be chosen through configuration changes alone at the time of deployment.
  11. </para>
  12. <para>
  13. Document databases and services are increasingly common in application development. These
  14. data sources are somewhat different from traditional relational data sources, as they eschew
  15. complex relationships for performance, scalability, and flexibility. Examples of
  16. document-oriented services include Amazon SimpleDB and Azure Table Storage.
  17. </para>
  18. <para>
  19. The Simple Cloud API offers some flexibility for vendor-specific features with an
  20. <varname>$options</varname> array in each method signature. Some adapters require certain
  21. options that also must be added to the <varname>$options</varname> array. It is a good
  22. practice to retrieve these options from a configuration file to maintain compatibility with
  23. all services and databases; unrecognized options will simply be discarded, making it
  24. possible to use different services based on environment.
  25. </para>
  26. <para>
  27. If more vendor-specific requirements are required, the developer should extend the specific
  28. <classname>Zend_Cloud_DocumentService</classname> adapter to add support for these features.
  29. In this manner, vendor-specific features can be called out in the application by referring
  30. to the Simple Cloud API extensions in the subclass of the Simple Cloud adapter.
  31. </para>
  32. <sect2 id="zend.cloud.documentservice.adapterinterface">
  33. <title>Zend_Cloud_DocumentService_Adapter Interface</title>
  34. <para>
  35. The <classname>Zend_Cloud_DocumentService_Adapter</classname> interface defines methods
  36. that each concrete document service adapter implements. The following adapters are
  37. shipped with the Simple Cloud API:
  38. </para>
  39. <itemizedlist>
  40. <listitem>
  41. <para>
  42. <ulink url="http://aws.amazon.com/simpledb/"><classname>Zend_Cloud_DocumentService_Adapter_SimpleDb</classname></ulink>
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. <ulink url="http://msdn.microsoft.com/en-us/library/dd179423.aspx"><classname>Zend_Cloud_DocumentService_Adapter_WindowsAzure</classname></ulink>
  48. </para>
  49. </listitem>
  50. </itemizedlist>
  51. <para>
  52. To instantiate a document service adapter, use the static method
  53. <methodname>Zend_Cloud_DocumentService_Factory::getAdapter()</methodname>, which accepts
  54. a configuration array or a <classname>Zend_Config</classname> object. The
  55. <varname>document_adapter</varname> key should specify the concrete adapter class by
  56. classname. Adapter-specific keys may also be passed in this configuration parameter.
  57. </para>
  58. <example id="zend.cloud.documentservice.factory.example">
  59. <title>Example: Using the SimpleDB adapter</title>
  60. <programlisting language="php"><![CDATA[
  61. $adapterClass = 'Zend_Cloud_DocumentService_Adapter_SimpleDb';
  62. $documents = Zend_Cloud_DocumentService_Factory::getAdapter(array(
  63. Zend_Cloud_DocumentService_Factory::DOCUMENT_ADAPTER_KEY => $adapterClass,
  64. Zend_Cloud_DocumentService_Adapter_SimpleDb::AWS_ACCESS_KEY => $amazonKey,
  65. Zend_Cloud_DocumentService_Adapter_SimpleDb::AWS_SECRET_KEY => $amazonSecret
  66. ));
  67. ]]></programlisting>
  68. </example>
  69. </sect2>
  70. <sect2 id="zend.cloud.documentservice.adapteroptions">
  71. <title>Supported Adapter Options</title>
  72. <table frame="all" id="zend.cloud.documentservice.options.general">
  73. <title>Zend_Cloud_DocumentService_Adapter Common Options</title>
  74. <tgroup cols="5">
  75. <thead>
  76. <row>
  77. <entry>Option key</entry>
  78. <entry>Description</entry>
  79. <entry>Used in</entry>
  80. <entry>Required</entry>
  81. <entry>Default</entry>
  82. </row>
  83. </thead>
  84. <tbody>
  85. <row>
  86. <entry>document_class</entry>
  87. <entry>
  88. <para>
  89. Class to use to represent returned documents. The class provided must extend
  90. <classname>Zend_Cloud_DocumentService_Document</classname> to ensure
  91. compatibility with all document services. For all methods that
  92. return a document or collection of documents, this class will be
  93. used.
  94. </para>
  95. </entry>
  96. <entry>Constructor</entry>
  97. <entry>No</entry>
  98. <entry><classname>Zend_Cloud_Document_Service_Document</classname></entry>
  99. </row>
  100. <row>
  101. <entry>documentset_class</entry>
  102. <entry>
  103. <para>
  104. Class to use to represent collections of documents,
  105. <classname>Zend_Cloud_DocumentService_DocumentSet</classname> by
  106. default. Typically, objects of this class will be returned by
  107. <methodname>listDocuments()</methodname> and
  108. <methodname>query()</methodname>. Any class provided for this
  109. configuration value must extend
  110. <classname>Zend_Cloud_DocumentService_DocumentSet</classname>.
  111. </para>
  112. </entry>
  113. <entry>Constructor</entry>
  114. <entry>No</entry>
  115. <entry><classname>Zend_Cloud_DocumentService_DocumentSet</classname></entry>
  116. </row>
  117. </tbody>
  118. </tgroup>
  119. </table>
  120. <table frame="all" id="zend.cloud.documentservice.options.sdb">
  121. <title>Zend_Cloud_DocumentService_Adapter_SimpleDb Options</title>
  122. <tgroup cols="5">
  123. <thead>
  124. <row>
  125. <entry>Option key</entry>
  126. <entry>Description</entry>
  127. <entry>Used in</entry>
  128. <entry>Required</entry>
  129. <entry>Default</entry>
  130. </row>
  131. </thead>
  132. <tbody>
  133. <row>
  134. <entry>query_class</entry>
  135. <entry>
  136. <para>
  137. Class to use for creating and assembling queries for this document
  138. service; <methodname>select()</methodname> will create objects of
  139. this class name, as will <methodname>listDocuments()</methodname>.
  140. </para>
  141. </entry>
  142. <entry>Constructor</entry>
  143. <entry>No</entry>
  144. <entry><classname>Zend_Cloud_DocumentService_Adapter_SimpleDb_Query</classname></entry>
  145. </row>
  146. <row>
  147. <entry>aws_accesskey</entry>
  148. <entry>Your Amazon AWS access key</entry>
  149. <entry>Constructor</entry>
  150. <entry>Yes</entry>
  151. <entry>None</entry>
  152. </row>
  153. <row>
  154. <entry>aws_secretkey</entry>
  155. <entry>Your Amazon AWS secret key</entry>
  156. <entry>Constructor</entry>
  157. <entry>Yes</entry>
  158. <entry>None</entry>
  159. </row>
  160. <row>
  161. <entry>http_adapter</entry>
  162. <entry>HTTP adapter to use in all access operations</entry>
  163. <entry>Constructor</entry>
  164. <entry>No</entry>
  165. <entry><classname>Zend_Http_Client_Adapter_Socket</classname></entry>
  166. </row>
  167. <row>
  168. <entry>merge</entry>
  169. <entry>
  170. <para>
  171. If a boolean true, all attribute values are merged. You may also
  172. specify an array of key pairs, where the key is the attribute key to
  173. merge, and the value indicates whether or not to merge; a boolean
  174. true value will merge the given key. Any attributes not specified in
  175. this array will be replaced.
  176. </para>
  177. </entry>
  178. <entry><methodname>updateDocument()</methodname></entry>
  179. <entry>No</entry>
  180. <entry>True</entry>
  181. </row>
  182. <row>
  183. <entry>return_documents</entry>
  184. <entry>
  185. <para>
  186. If a boolean true, <methodname>query()</methodname> returns a
  187. <classname>Zend_Cloud_DocumentService_DocumentSet</classname> object
  188. containing
  189. <classname>Zend_Cloud_DocumentService_Document</classname> objects
  190. (default case); otherwise, it returns an array of arrays.
  191. </para>
  192. </entry>
  193. <entry><methodname>query()</methodname></entry>
  194. <entry>No</entry>
  195. <entry>True</entry>
  196. </row>
  197. </tbody>
  198. </tgroup>
  199. </table>
  200. <table frame='all' id="zend.cloud.documentservice.options.azure">
  201. <title>Zend_Cloud_DocumentService_Adapter_WindowsAzure Options</title>
  202. <tgroup cols="5">
  203. <thead>
  204. <row>
  205. <entry>Option key</entry>
  206. <entry>Description</entry>
  207. <entry>Used in</entry>
  208. <entry>Required</entry>
  209. <entry>Default</entry>
  210. </row>
  211. </thead>
  212. <tbody>
  213. <row>
  214. <entry>query_class</entry>
  215. <entry>
  216. <para>
  217. Class to use for creating and assembling queries for this document
  218. service; <methodname>select()</methodname> will create objects of
  219. this class name, as will <methodname>listDocuments()</methodname>.
  220. </para>
  221. </entry>
  222. <entry>Constructor</entry>
  223. <entry>No</entry>
  224. <entry><classname>Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query</classname></entry>
  225. </row>
  226. <row>
  227. <entry>default_partition_key</entry>
  228. <entry>
  229. <para>
  230. The default partition key to use if none is specified in the
  231. document identifier. Windows Azure requires a two-fold document ID,
  232. consisting of a PartitionKey and a RowKey. The PartitionKey will
  233. typically be common across your database or a collection, while the
  234. RowKey will vary. As such, this setting allows you to specify the
  235. default PartitionKey to utilize for all documents.
  236. </para>
  237. <para>
  238. If not specified, the adapter will default to using the collection
  239. name as the PartitionKey.
  240. </para>
  241. </entry>
  242. <entry>Constructor, <methodname>setDefaultPartitionKey()</methodname></entry>
  243. <entry>Name of whatever collection the document belongs to</entry>
  244. </row>
  245. <row>
  246. <entry>storage_accountname</entry>
  247. <entry>Windows Azure account name</entry>
  248. <entry>Constructor</entry>
  249. <entry>Yes</entry>
  250. <entry>None</entry>
  251. </row>
  252. <row>
  253. <entry>storage_accountkey</entry>
  254. <entry>Windows Azure account key</entry>
  255. <entry>Constructor</entry>
  256. <entry>Yes</entry>
  257. <entry>None</entry>
  258. </row>
  259. <row>
  260. <entry>storage_host</entry>
  261. <entry>
  262. <para>
  263. Windows Azure access host, default is
  264. <varname>table.core.windows.net</varname>
  265. </para>
  266. </entry>
  267. <entry>Constructor</entry>
  268. <entry>No</entry>
  269. <entry><varname>table.core.windows.net</varname></entry>
  270. </row>
  271. <row>
  272. <entry>storage_proxy_host</entry>
  273. <entry>Proxy hostname</entry>
  274. <entry>Constructor</entry>
  275. <entry>No</entry>
  276. <entry>None</entry>
  277. </row>
  278. <row>
  279. <entry>storage_proxy_port</entry>
  280. <entry>Proxy port</entry>
  281. <entry>Constructor</entry>
  282. <entry>No</entry>
  283. <entry>8080</entry>
  284. </row>
  285. <row>
  286. <entry>storage_proxy_credentials</entry>
  287. <entry>Proxy credentials</entry>
  288. <entry>Constructor</entry>
  289. <entry>No</entry>
  290. <entry>None</entry>
  291. </row>
  292. <row>
  293. <entry>HTTP Adapter</entry>
  294. <entry>HTTP adapter to use in all access operations</entry>
  295. <entry>Constructor</entry>
  296. <entry>No</entry>
  297. <entry>None</entry>
  298. </row>
  299. <row>
  300. <entry>verify_etag</entry>
  301. <entry>
  302. <para>
  303. Verify ETag on the target document and perform the operation only if the
  304. ETag matches the expected value
  305. </para>
  306. </entry>
  307. <entry>
  308. <methodname>updateDocument()</methodname>,
  309. <methodname>replaceDocument()</methodname>,
  310. <methodname>deleteDocument()</methodname>
  311. </entry>
  312. <entry>No</entry>
  313. <entry>False</entry>
  314. </row>
  315. </tbody>
  316. </tgroup>
  317. </table>
  318. </sect2>
  319. <sect2 id="zend.cloud.documentservice.concepts">
  320. <title>Basic concepts</title>
  321. <para>
  322. Each document-oriented service and database uses its own terminology and constructs in
  323. its API. The SimpleCloud API identifies and abstracts a number of common concepts and
  324. operations that are shared among providers.
  325. </para>
  326. <para>
  327. Document storage consists of a number of <emphasis>collections</emphasis>, which are
  328. logical storage units analogous to database tables in the SQL world. Collections contain
  329. <emphasis>documents</emphasis>, which are essentially a set of key-value pairs, along
  330. with some metadata specific to the storage engine, and are identified by a unique
  331. <emphasis>document ID</emphasis>.
  332. </para>
  333. <para>
  334. Each document has its own structure (set of fields) that does not necessarily have to
  335. match the structure of any other document, even in the same collection. In fact, you can
  336. change this structure after the document is created.
  337. </para>
  338. <para>
  339. Documents can be retrieved by ID or by querying a collection.
  340. </para>
  341. <para>
  342. Documents are represented by the class
  343. <classname>Zend_Cloud_DocumentService_Document</classname>. Note that the document
  344. class does not validate the supplied IDs and data, and does not enforce compatibility
  345. with each adapter's requirements.
  346. </para>
  347. <para>
  348. The document fields can be accessed using keys as object properties and as array
  349. elements.
  350. </para>
  351. <para>
  352. The basic interface of <classname>Zend_Cloud_DocumentService_Document</classname> is as
  353. follows:
  354. </para>
  355. <programlisting language="php"><![CDATA[
  356. /**
  357. * ArrayAccess allows accessing fields by array key:
  358. * $doc['fieldname']
  359. *
  360. * IteratorAggregate allows iterating over all fields:
  361. * foreach ($document as $field => $value) {
  362. * echo "$field: $value\n";
  363. * }
  364. *
  365. * Countable provides a count of all fields:
  366. * count($document)
  367. */
  368. class Zend_Cloud_DocumentService_Document
  369. implements ArrayAccess, IteratorAggregate, Countable
  370. {
  371. const KEY_FIELD = '_id';
  372. /**
  373. * $fields may be an array or an object implementing ArrayAccess.
  374. * If no $id is provided, it will look for a field matching KEY_FIELD to
  375. * use as the identifier.
  376. */
  377. public function __construct($fields, $id = null);
  378. public function setId($id);
  379. public function getId();
  380. public function getFields();
  381. public function getField($name);
  382. public function setField($name, $value);
  383. /**
  384. * These allow overloading, so you may access fields as if they were
  385. * native properties of the document
  386. */
  387. public function __get($name);
  388. public function __set($name, $value);
  389. /**
  390. * Alternately, you can acces fields as if via native getters and
  391. * setters:
  392. * $document->setFoo($value); // set "Foo" field to value
  393. * $value = $document->getFoo(); // get "Foo" field value
  394. public function __call($name, $args);
  395. }
  396. ]]></programlisting>
  397. <note>
  398. <title>Windows Azure Document Identifiers</title>
  399. <para>
  400. Windows Azure technically requires a combination of two fields to uniquely
  401. identify documents: the <varname>PartitionKey</varname> and
  402. <varname>RowKey</varname>, and as such, keys are fully qualified by the structure
  403. <code>array(PartitionKey, RowKey)</code> -- which makes them non-portable. In most
  404. situations, the <varname>PartitionKey</varname> will not differ for documents in a
  405. single collection -- and potentially not even across your entire table instance. As
  406. such, the DocumentService provides several options for specifying keys:
  407. </para>
  408. <itemizedlist>
  409. <listitem>
  410. <para>
  411. Array keys will always work as expected.
  412. </para>
  413. </listitem>
  414. <listitem>
  415. <para>
  416. If a string key is provided:
  417. </para>
  418. <itemizedlist>
  419. <listitem>
  420. <para>
  421. If the <varname>default_partition_key</varname> setting was provided
  422. to the constructor, or passed to the
  423. <methodname>setDefaultPartitionKey()</methodname> method, that value
  424. will be used for the <varname>PartitionKey</varname>.
  425. </para>
  426. </listitem>
  427. <listitem>
  428. <para>
  429. Otherwise, the name of the collection on which you are operating
  430. will be used.
  431. </para>
  432. </listitem>
  433. </itemizedlist>
  434. </listitem>
  435. </itemizedlist>
  436. <para>
  437. The takeaway is that you can utilize string keys if you wish to maximize portability
  438. of your application. Just be aware that your record will contain a few extra fields
  439. to denote the key (<varname>PartitionKey</varname>, <varname>RowKey</varname>, and
  440. the previously undiscussed <varname>Timestamp</varname>) should you ever migrate
  441. your data to another service.
  442. </para>
  443. </note>
  444. <example id="zend.cloud.documentservice.document.create.example">
  445. <title>Creating a document</title>
  446. <programlisting language="php"><![CDATA[
  447. $document = new Zend_Cloud_DocumentService_Document(array(
  448. 'key1' => 'value1',
  449. 'key2' => 123,
  450. 'key3' => 'thirdvalue',
  451. ), "DocumentId");
  452. $document->otherkey = 'some more data';
  453. echo "key 1: " . $document->key1 . "\n"; // object notation
  454. echo "key 2: " . $document['key2'] . "\n"; // array notation
  455. ]]></programlisting>
  456. </example>
  457. <example id="zend.cloud.documentservice.document.explore.example">
  458. <title>Exploring the document data</title>
  459. <programlisting language="php"><![CDATA[
  460. $document = $documents->fetchDocument("mydata", $id);
  461. echo "Document ID: " . $document->getID() . "\n";
  462. foreach ($document->getFields() as $key => $value) {
  463. echo "Field $key is $value\n";
  464. }
  465. ]]></programlisting>
  466. </example>
  467. </sect2>
  468. <sect2 id="zend.cloud.documentservice.exceptions">
  469. <title>Exceptions</title>
  470. <para>
  471. If some error occurs in the document service,
  472. <classname>Zend_Cloud_DocumentService_Exception</classname> is thrown. If the exception
  473. was caused by the underlying service driver, you can use the
  474. <methodname>getClientException()</methodname> method to retrieve the original exception.
  475. </para>
  476. <para>
  477. Since different cloud providers implement different sets of services, some drivers do
  478. not implement certain features. In this case, the
  479. <classname>Zend_Cloud_OperationNotAvailableException</classname> exception is thrown.
  480. </para>
  481. </sect2>
  482. <sect2 id="zend.cloud.documentservice.create-collection">
  483. <title>Creating a collection</title>
  484. <para>
  485. A new collection is created using <methodname>createCollection()</methodname>.
  486. </para>
  487. <example id="zend.cloud.documentservice.create-collection.example">
  488. <title>Creating collection</title>
  489. <programlisting language="php"><![CDATA[
  490. $documents->createCollection("mydata");
  491. ]]></programlisting>
  492. </example>
  493. <para>
  494. If you call <methodname>createCollection()</methodname> with a collection name that
  495. already exists, the service will do nothing and leave the existing collection untouched.
  496. </para>
  497. </sect2>
  498. <sect2 id="zend.cloud.documentservice.delete-collection">
  499. <title>Deleting a collection</title>
  500. <para>
  501. A collection is deleted by calling <methodname>deleteCollection()</methodname>.
  502. </para>
  503. <example id="zend.cloud.documentservice.delete-collection.example">
  504. <title>Deleting a collection</title>
  505. <programlisting language="php"><![CDATA[
  506. $documents->deleteCollection("mydata");
  507. ]]></programlisting>
  508. </example>
  509. <para>
  510. Deleting a collection automatically deletes all documents contained in that collection.
  511. </para>
  512. <note>
  513. <para>
  514. Deleting a collection can take significant time for some services. You cannot
  515. re-create a collection with the same name until the collection and all its documents
  516. have been completely removed.
  517. </para>
  518. </note>
  519. <para>
  520. Deleting a non-existent collection will have no effect.
  521. </para>
  522. </sect2>
  523. <sect2 id="zend.cloud.documentservice.list-collections">
  524. <title>Listing available collections</title>
  525. <para>
  526. A list of existing collections is returned by
  527. <methodname>listCollections()</methodname>. This method returns an array of all the
  528. names of collections belonging to the account you specified when you created the
  529. adapter.
  530. </para>
  531. <example id="zend.cloud.documentservice.list-collections.example">
  532. <title>List collections</title>
  533. <programlisting language="php"><![CDATA[
  534. $list = $documents->listCollections();
  535. foreach ($list as $collection) {
  536. echo "My collection: $collection\n";
  537. }
  538. ]]></programlisting>
  539. </example>
  540. </sect2>
  541. <sect2 id="zend.cloud.documentservice.insert">
  542. <title>Inserting a document</title>
  543. <para>
  544. To insert a document, you need to provide a
  545. <classname>Zend_Cloud_DocumentService_Document</classname> object or associative array
  546. of data, as well as the collection in which you are inserting it.
  547. </para>
  548. <para>
  549. Many providers require that you provide a document ID with your document. If using a
  550. <classname>Zend_Cloud_DocumentService_Document</classname>, you can specify this by
  551. passing the identifier to the constructor when you instantiate the object. If using an
  552. associative array, the key name will be adapter-specific locations; for example, on
  553. Azure, the ID is made up of the PartitionKey and RowKey; on Amazon SimpleDB, the ID is
  554. the ItemName; you may also specify the key in the <varname>_id</varname> field to be
  555. more portable.
  556. </para>
  557. <para>
  558. As such, the easiest and most compatible way to specify the key is to use
  559. a Document object.
  560. </para>
  561. <example id="zend.cloud.documentservice.insert.example">
  562. <title>Inserting a document</title>
  563. <programlisting language="php"><![CDATA[
  564. // Instantiating and creating the document
  565. $document = new Zend_Cloud_DocumentService_Document(array(
  566. 'key1' => 'value1',
  567. 'key2' => 123,
  568. 'key3' => 'thirdvalue',
  569. ), "DocumentID");
  570. // inserting into the "mydata" collection
  571. $documents->insertDocument("mydata", $document);
  572. ]]></programlisting>
  573. </example>
  574. </sect2>
  575. <sect2 id="zend.cloud.documentservice.replace">
  576. <title>Replacing a document</title>
  577. <para>
  578. <emphasis>Replacing</emphasis> a document means removing all document data associated with a particular
  579. document key and substituting it with a new set of data. Unlike
  580. <emphasis>updating</emphasis>, this operation does not merge old and new data but
  581. replaces the document as a whole. The replace operation, like
  582. <methodname>insertDocument()</methodname>, accepts a
  583. <classname>Zend_Cloud_DocumentService_Document</classname> document or an array of
  584. key-value pairs that specify names and values of the new fields, and the collection in
  585. which the document exists.
  586. </para>
  587. <note>
  588. <title>Document ID is required</title>
  589. <para>
  590. To replace the document, the document ID is required. Just like inserting a document,
  591. if you use an associative array to describe the document, you will need to provide a
  592. provider-specific key indicating the document ID. As such, the most compatible way
  593. to replace a document across providers is to utilize a Document object, as shown in
  594. the examples.
  595. </para>
  596. </note>
  597. <example id="zend.cloud.documentservice.replace.example">
  598. <title>Replacing a document</title>
  599. <programlisting language="php"><![CDATA[
  600. $document = new Zend_Cloud_DocumentService_Document(array(
  601. 'key1' => 'value1',
  602. 'key2' => 123,
  603. 'key3' => 'thirdvalue',
  604. ), "DocumentID");
  605. // Update the document as found in the "mydata" collection
  606. $documents->replaceDocument("mydata", $document);
  607. ]]></programlisting>
  608. <para>
  609. You may also use an existing Document object, re-assign the fields and/or assign new
  610. fields, and pass it to the <methodname>replaceDocument()</methodname> method:
  611. </para>
  612. <programlisting language="php"><![CDATA[
  613. $docment->key4 = '4th value';
  614. // Update the document as found in the "mydata" collection
  615. $documents->replaceDocument("mydata", $document);
  616. ]]></programlisting>
  617. </example>
  618. </sect2>
  619. <sect2 id="zend.cloud.documentservice.update">
  620. <title>Updating a document</title>
  621. <para>
  622. <emphasis>Updating</emphasis> a document changes the key/value pairs in an existing
  623. document. This operation does not share the <emphasis>replace</emphasis> semantics; the
  624. values of the keys that are not specified in the data set will not be changed. You must
  625. provide both a document key and data, either via a
  626. <classname>Zend_Cloud_DocumentService_Document</classname> document or an array, to this
  627. method. If the key is null and a document object is provided, the document key is used.
  628. </para>
  629. <example id="zend.cloud.documentservice.update.example">
  630. <title>Updating a document</title>
  631. <programlisting language="php"><![CDATA[
  632. // update one field
  633. $documents->updateDocument("mydata", "DocumentID", array("key2" => "new value"));
  634. // or with document; this could be a document already retrieved from the service
  635. $document = new Zend_Cloud_DocumentService_Document(array(
  636. 'key1' => 'value1',
  637. 'key2' => 123,
  638. 'key3' => 'thirdvalue',
  639. ), "DocumentID");
  640. $documents->updateDocument("mydata", null, $document);
  641. // copy document to another ID
  642. $documents->updateDocument("mydata", "AnotherDocumentID", $document);
  643. ]]></programlisting>
  644. </example>
  645. <para>
  646. Amazon SimpleDB supports multi-value fields, so data updates will be merged with the old key
  647. value instead of replacing them. Option <property>merge</property> should contain an array
  648. of field names to be merged. The array should be key/value pairs, with the key
  649. corresponding to the field key, and the value a boolean value indicating merge status
  650. (boolean true would merge; false would not). Any keys not specified in the
  651. <property>merge</property> option will be replaced instead of merged.
  652. </para>
  653. <example id="zend.cloud.documentservice.update.merge.example">
  654. <title>Merging document fields</title>
  655. <programlisting language="php"><![CDATA[
  656. // key2 is overwritten, key3 is merged
  657. $documents->updateDocument('mydata', 'DocumentID',
  658. array('key2' => 'new value', 'key3' => 'additional value'),
  659. array('merge' => array('key3' => true))
  660. );
  661. ]]></programlisting>
  662. </example>
  663. </sect2>
  664. <sect2 id="zend.cloud.documentservice.delete">
  665. <title>Deleting a document</title>
  666. <para>
  667. A document can be deleted by passing its key to
  668. <methodname>deleteDocument()</methodname>. Deleting a non-existant document has no
  669. effect.
  670. </para>
  671. <example id="zend.cloud.documentservice.delete.example">
  672. <title>Deleting a document</title>
  673. <programlisting language="php"><![CDATA[
  674. $documents->deleteDocument("collectionName", "DocumentID");
  675. ]]></programlisting>
  676. </example>
  677. </sect2>
  678. <sect2 id="zend.cloud.documentservice.fetch">
  679. <title>Fetching a document</title>
  680. <para>
  681. You can fetch a specific document by specifying its key.
  682. <methodname>fetchDocument()</methodname> returns one instance of
  683. <classname>Zend_Cloud_DocumentService_Document</classname>.
  684. </para>
  685. <example id="zend.cloud.documentservice.fetch.example">
  686. <title>Fetching a document</title>
  687. <programlisting language="php"><![CDATA[
  688. $document = $service->fetchDocument('collectionName', 'DocumentID');
  689. echo "Document ID: " . var_export($document->getID(), 1) . "\n";
  690. foreach ($document->getFields() as $key => $value) {
  691. echo "Field $key is $value\n";
  692. }
  693. ]]></programlisting>
  694. </example>
  695. </sect2>
  696. <sect2 id="zend.cloud.documentservice.query">
  697. <title>Querying a collection</title>
  698. <para>
  699. To find documents in the collection that meet some criteria, use the
  700. <methodname>query()</methodname>method. This method accepts either a string which is an
  701. adapter-dependent query and is passed as-is to the concrete adapter, or a structured query
  702. object instance of <classname>Zend_Cloud_DocumentService_Query</classname>. The return
  703. is a <classname>Zend_Cloud_DocumentService_DocumentSet</classname>, containing instances
  704. of <classname>Zend_Cloud_DocumentService_Document</classname> that satisfy the query.
  705. The DocumentSet object is iterable and countable.
  706. </para>
  707. <example id="zend.cloud.documentservice.query.example">
  708. <title>Querying a collection using a string query</title>
  709. <programlisting language="php"><![CDATA[
  710. $docs = $documents->query(
  711. "collectionName",
  712. "RowKey eq 'rowkey2' or RowKey eq 'rowkey2'"
  713. );
  714. foreach ($docs as $doc) {
  715. $id = $doc->getId();
  716. echo "Found document with ID: "
  717. . var_export($id, 1)
  718. . "\n";
  719. }
  720. ]]></programlisting>
  721. </example>
  722. <para>
  723. If using a structured query object, typically, you will retrieve it using the
  724. <methodname>select()</methodname> method. This ensures that the query object is specific
  725. to your adapter, which will ensure that it is assembled into a syntax your adapter
  726. understands.
  727. </para>
  728. <example id="zend.cloud.documentservice.query.struct-example">
  729. <title>Querying a collection with structured query</title>
  730. <programlisting language="php"><![CDATA[
  731. $query = $service->select();
  732. $query->from('collectionName')
  733. ->where('year > ?', array(1945))
  734. ->limit(3);
  735. $docs = $documents->query('collectionName', $query);
  736. foreach ($docs as $doc) {
  737. $id = $doc->getId();
  738. echo "Found document with ID: "
  739. . var_export($id, 1)
  740. . "\n";
  741. }
  742. ]]></programlisting>
  743. </example>
  744. <para>
  745. <classname>Zend_Cloud_DocumentService_Query</classname> classes do not limit which query
  746. clauses can be used, but the clause must be supported by the underlying concrete
  747. adapter. Currently supported clauses include:
  748. </para>
  749. <itemizedlist>
  750. <listitem>
  751. <para>
  752. <methodname>select()</methodname> - defines which fields are returned in the
  753. result.
  754. </para>
  755. <note>
  756. <para>
  757. Windows Azure ignores this clause's argument and always returns the whole
  758. document.
  759. </para>
  760. </note>
  761. </listitem>
  762. <listitem>
  763. <para>
  764. <methodname>from()</methodname> - defines the collection name used in the query.
  765. </para>
  766. </listitem>
  767. <listitem>
  768. <para>
  769. <methodname>where()</methodname> - defines the conditions of the query. It
  770. accepts three parameters: condition, array of arguments to replace "?" fields in
  771. the condition, and a conjunction argument which should be "and" or "or", and
  772. which will be used to join this condition with previous conditions. Multiple
  773. <methodname>where()</methodname> clasues may be specified.
  774. </para>
  775. </listitem>
  776. <listitem>
  777. <para>
  778. <methodname>whereId()</methodname> - defines the condition by document ID (key).
  779. The document matching must have the same key. The method accepts one argument -
  780. the required ID (key).
  781. </para>
  782. </listitem>
  783. <listitem>
  784. <para>
  785. <methodname>limit()</methodname> - limits the returned data to specified number
  786. of documents.
  787. </para>
  788. </listitem>
  789. <listitem>
  790. <para>
  791. <methodname>order()</methodname> - sorts the returned data by specified field.
  792. Accepts two arguments - first is the field name and second is 'asc' or 'desc'
  793. specifying the sort direction.
  794. </para>
  795. <note>
  796. <para>
  797. This clause is not currently supported by Windows Azure.
  798. </para>
  799. </note>
  800. </listitem>
  801. </itemizedlist>
  802. </sect2>
  803. <sect2 id="zend.cloud.documentservice.select">
  804. <title>Creating a query</title>
  805. <para>
  806. For the user's convenience, the <methodname>select()</methodname> method instantiates a
  807. query object specific to the adapter, and sets the SELECT clause for it.
  808. </para>
  809. <example id="zend.cloud.documentservice.select.example">
  810. <title>Creating a structured query</title>
  811. <programlisting language="php"><![CDATA[
  812. $query = $documents->select()
  813. ->from('collectionName')
  814. ->where('year > ?', array(1945))
  815. ->limit(3);
  816. $docs = $documents->query('collectionName', $query);
  817. foreach ($docs as $doc) {
  818. $id = $doc->getId();
  819. echo "Found document with ID: "
  820. . var_export($id, 1)
  821. . "\n";
  822. }
  823. ]]></programlisting>
  824. </example>
  825. </sect2>
  826. <sect2 id="zend.cloud.documentservice.adapter">
  827. <title>Accessing concrete adapters</title>
  828. <para>
  829. Sometimes it is necessary to retrieve the concrete adapter for the service that the
  830. Document API is working with. This can be achieved by using the
  831. <methodname>getAdapter()</methodname> method.
  832. </para>
  833. <note>
  834. <para>
  835. Accessing the underlying adapter breaks portability among services, so it should be
  836. reserved for exceptional circumstances only.
  837. </para>
  838. </note>
  839. <example id="zend.cloud.documentservice.adapter.example">
  840. <title>Using concrete adapters</title>
  841. <programlisting language="php"><![CDATA[
  842. // Since SimpleCloud Document API doesn't support batch upload, use concrete adapter
  843. $amazonSdb = $documents->getAdapter();
  844. $amazonSdb->batchPutAttributes($items, 'collectionName');
  845. ]]></programlisting>
  846. </example>
  847. </sect2>
  848. </sect1>