Zend_Service_WindowsAzure_Table.xml 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19991 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.service.windowsazure.storage.table">
  5. <title>Zend_Service_WindowsAzure_Storage_Table</title>
  6. <para>
  7. Der Table Service bietet einen strukturierten Speicher in der Form von Tabellen.
  8. </para>
  9. <para>
  10. Tabellen Speicher wird von Windows Azure als REST API angeboten die von der Klasse
  11. <classname>Zend_Service_WindowsAzure_Storage_Table</classname> umhüllt ist um ein natives
  12. PHP Interface zum Speicher Konto zu bieten.
  13. </para>
  14. <para>
  15. Dieses Thema zeigt einige Beispiele der Verwendung der Klasse
  16. <classname>Zend_Service_WindowsAzure_Storage_Table</classname>. Andere Features sind im
  17. Download Paket enthalten sowie in den detailierten API Dokumentationen dieser Features.
  18. </para>
  19. <para>
  20. Es ist zu beachten das bei der Entwicklung der Tabellen Speicher (in der SDK von Windows
  21. Azure) nicht alle Features unterstützt welche von dieser API angeboten werden. Deshalb sind
  22. die Beispiele welche auf dieser Seite aufgeführt sind, dazu gedacht auf Windows Azure
  23. Produktions Tabellen Speichern verwendet zu werden.
  24. </para>
  25. <sect2 id="zend.service.windowsazure.storage.table.api">
  26. <title>Operationen auf Tabellen</title>
  27. <para>
  28. Dieses Thema zeigt einige Beispiele für Operationen welche auf Tabellen ausgeführt
  29. werden können.
  30. </para>
  31. <sect3 id="zend.service.windowsazure.storage.table.api.create">
  32. <title>Erstellung einer Tabelle</title>
  33. <para>
  34. Bei Verwendung des folgenden Codes, kann eine Tabelle auf dem Windows Azure
  35. Produktions Tabellen Speicher erstellt werden.
  36. </para>
  37. <example id="zend.service.windowsazure.storage.table.api.create.example">
  38. <title>Erstellen einer Tabelle</title>
  39. <programlisting language="php"><![CDATA[
  40. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  41. 'table.core.windows.net', 'myaccount', 'myauthkey'
  42. );
  43. $result = $storageClient->createTable('testtable');
  44. echo 'Der neue Name der Tabelle ist: ' . $result->Name;
  45. ]]></programlisting>
  46. </example>
  47. </sect3>
  48. <sect3 id="zend.service.windowsazure.storage.table.api.list">
  49. <title>Ausgeben aller Tabellen</title>
  50. <para>
  51. Bei Verwendung des folgendes Codes, kann eine Liste alle Tabellen im Windows Azure
  52. Produktions Tabellen Speicher abgefragt werden.
  53. </para>
  54. <example id="zend.service.windowsazure.storage.table.api.list.example">
  55. <title>Ausgeben aller Tabellen</title>
  56. <programlisting language="php"><![CDATA[
  57. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  58. 'table.core.windows.net', 'myaccount', 'myauthkey'
  59. );
  60. $result = $storageClient->listTables();
  61. foreach ($result as $table) {
  62. echo 'Der Name der Tabelle ist: ' . $table->Name . "\r\n";
  63. }
  64. ]]></programlisting>
  65. </example>
  66. </sect3>
  67. </sect2>
  68. <sect2 id="zend.service.windowsazure.storage.table.entities">
  69. <title>Operationen auf Entitäten</title>
  70. <para>
  71. Tabellen speichern Daten als Sammlung von Entitäten. Entitäten sind so ähnlich wie
  72. Zeilen. Eine Entität hat einen primären Schlüssel und ein Set von Eigenschaften. Eine
  73. Eigenschaft ist ein benanntes, Typ-Werte Paar, ähnlich einer Spalte.
  74. </para>
  75. <para>
  76. Der Tabellen Service erzwingt kein Schema für Tabellen, deshalb können zwei Entitäten in
  77. der selben Tabelle unterschiedliche Sets von Eigenschaften haben. Entwickler können
  78. auswählen das ein Schema auf Seite des Clients erzwungen wird. Eine Tabelle kann eine
  79. beliebige Anzahl an Entitäten enthalten.
  80. </para>
  81. <para>
  82. <classname>Zend_Service_WindowsAzure_Storage_Table</classname> bietet 2 Wege um mit
  83. Entitäten zu arbeiten:
  84. </para>
  85. <itemizedlist>
  86. <listitem>
  87. <para>
  88. Erzwungenes Schema
  89. </para>
  90. </listitem>
  91. <listitem>
  92. <para>
  93. Nicht erzwungenes Schema
  94. </para>
  95. </listitem>
  96. </itemizedlist>
  97. <para>
  98. Alle Beispiel verwenden die folgende erwzungene Schema Klasse.
  99. </para>
  100. <example id="zend.service.windowsazure.storage.table.entities.schema">
  101. <title>Erzwungenes Schema welches in Beispielen verwendet wird</title>
  102. <programlisting language="php"><![CDATA[
  103. class SampleEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
  104. {
  105. /**
  106. * @azure Name
  107. */
  108. public $Name;
  109. /**
  110. * @azure Age Edm.Int64
  111. */
  112. public $Age;
  113. /**
  114. * @azure Visible Edm.Boolean
  115. */
  116. public $Visible = false;
  117. }
  118. ]]></programlisting>
  119. </example>
  120. <para>
  121. Note that if no schema class is passed into table storage methods,
  122. <classname>Zend_Service_WindowsAzure_Storage_Table</classname> automatically works with
  123. <classname>Zend_Service_WindowsAzure_Storage_DynamicTableEntity</classname>.
  124. </para>
  125. <sect3 id="zend.service.windowsazure.storage.table.entities.enforced">
  126. <title>Enforced schema entities</title>
  127. <para>
  128. To enforce a schema on the client side using the
  129. <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class, you can create
  130. a class which inherits
  131. <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname>. This class
  132. provides some basic functionality for the
  133. <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class to work with a
  134. client-side schema.
  135. </para>
  136. <para>
  137. Base properties provided by
  138. <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname> are:
  139. </para>
  140. <itemizedlist>
  141. <listitem>
  142. <para>
  143. PartitionKey (exposed through <methodname>getPartitionKey()</methodname> and
  144. <methodname>setPartitionKey()</methodname>)
  145. </para>
  146. </listitem>
  147. <listitem>
  148. <para>
  149. RowKey (exposed through <methodname>getRowKey()</methodname> and
  150. <methodname>setRowKey()</methodname>)
  151. </para>
  152. </listitem>
  153. <listitem>
  154. <para>
  155. Timestamp (exposed through <methodname>getTimestamp()</methodname> and
  156. <methodname>setTimestamp()</methodname>)
  157. </para>
  158. </listitem>
  159. <listitem>
  160. <para>
  161. Etag value (exposed through <methodname>getEtag()</methodname> and
  162. <methodname>setEtag()</methodname>)
  163. </para>
  164. </listitem>
  165. </itemizedlist>
  166. <para>
  167. Here's a sample class inheriting
  168. <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname>:
  169. </para>
  170. <example id="zend.service.windowsazure.storage.table.entities.enforced.schema">
  171. <title>Sample enforced schema class</title>
  172. <programlisting language="php"><![CDATA[
  173. class SampleEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
  174. {
  175. /**
  176. * @azure Name
  177. */
  178. public $Name;
  179. /**
  180. * @azure Age Edm.Int64
  181. */
  182. public $Age;
  183. /**
  184. * @azure Visible Edm.Boolean
  185. */
  186. public $Visible = false;
  187. }
  188. ]]></programlisting>
  189. </example>
  190. <para>
  191. The <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class will map
  192. any class inherited from
  193. <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname> to Windows
  194. Azure table storage entities with the correct data type and property name. All there
  195. is to storing a property in Windows Azure is adding a docblock comment to a public
  196. property or public getter/setter, in the following format:
  197. </para>
  198. <example id="zend.service.windowsazure.storage.table.entities.enforced.schema-property">
  199. <title>Enforced property</title>
  200. <programlisting language="php"><![CDATA[
  201. /**
  202. * @azure <property name in Windows Azure> <optional property type>
  203. */
  204. public $<property name in PHP>;
  205. ]]></programlisting>
  206. </example>
  207. <para>
  208. Let's see how to define a propety "Age" as an integer on Windows Azure table
  209. storage:
  210. </para>
  211. <example id="zend.service.windowsazure.storage.table.entities.enforced.schema-property-sample">
  212. <title>Sample enforced property</title>
  213. <programlisting language="php"><![CDATA[
  214. /**
  215. * @azure Age Edm.Int64
  216. */
  217. public $Age;
  218. ]]></programlisting>
  219. </example>
  220. <para>
  221. Note that a property does not necessarily have to be named the same on Windows Azure
  222. table storage. The Windows Azure table storage property name can be defined as well
  223. as the type.
  224. </para>
  225. <para>
  226. The following data types are supported:
  227. </para>
  228. <itemizedlist>
  229. <listitem>
  230. <para>
  231. <constant>Edm.Binary</constant> - An array of bytes up to 64 KB in size.
  232. </para>
  233. </listitem>
  234. <listitem>
  235. <para>
  236. <constant>Edm.Boolean</constant> - A boolean value.
  237. </para>
  238. </listitem>
  239. <listitem>
  240. <para>
  241. <constant>Edm.DateTime</constant> - A 64-bit value expressed as Coordinated
  242. Universal Time (UTC). The supported DateTime range begins from 12:00
  243. midnight, January 1, 1601 A.D. (C.E.), Coordinated Universal Time (UTC). The
  244. range ends at December 31st, 9999.
  245. </para>
  246. </listitem>
  247. <listitem>
  248. <para>
  249. <constant>Edm.Double</constant> - A 64-bit floating point value.
  250. </para>
  251. </listitem>
  252. <listitem>
  253. <para>
  254. <constant>Edm.Guid</constant> - A 128-bit globally unique identifier.
  255. </para>
  256. </listitem>
  257. <listitem>
  258. <para>
  259. <constant>Edm.Int32</constant> - A 32-bit integer.
  260. </para>
  261. </listitem>
  262. <listitem>
  263. <para>
  264. <constant>Edm.Int64</constant> - A 64-bit integer.
  265. </para>
  266. </listitem>
  267. <listitem>
  268. <para>
  269. <constant>Edm.String</constant> - A UTF-16-encoded value. String values may
  270. be up to 64 KB in size.
  271. </para>
  272. </listitem>
  273. </itemizedlist>
  274. </sect3>
  275. <sect3 id="zend.service.windowsazure.storage.table.entities.dynamic">
  276. <title>No enforced schema entities (a.k.a. DynamicEntity)</title>
  277. <para>
  278. To use the <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class
  279. without defining a schema, you can make use of the
  280. <classname>Zend_Service_WindowsAzure_Storage_DynamicTableEntity</classname> class.
  281. This class inherits <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname>
  282. like an enforced schema class does, but contains additional logic to make it dynamic
  283. and not bound to a schema.
  284. </para>
  285. <para>
  286. Base properties provided by
  287. <classname>Zend_Service_WindowsAzure_Storage_DynamicTableEntity</classname> are:
  288. </para>
  289. <itemizedlist>
  290. <listitem>
  291. <para>
  292. PartitionKey (exposed through <methodname>getPartitionKey()</methodname> and
  293. <methodname>setPartitionKey()</methodname>)
  294. </para>
  295. </listitem>
  296. <listitem>
  297. <para>
  298. RowKey (exposed through <methodname>getRowKey()</methodname> and
  299. <methodname>setRowKey()</methodname>)
  300. </para>
  301. </listitem>
  302. <listitem>
  303. <para>
  304. Timestamp (exposed through <methodname>getTimestamp()</methodname> and
  305. <methodname>setTimestamp()</methodname>)
  306. </para>
  307. </listitem>
  308. <listitem>
  309. <para>
  310. Etag value (exposed through <methodname>getEtag()</methodname> and
  311. <methodname>setEtag()</methodname>)
  312. </para>
  313. </listitem>
  314. </itemizedlist>
  315. <para>
  316. Other properties can be added on the fly. Their Windows Azure table storage type
  317. will be determined on-the-fly:
  318. </para>
  319. <example id="zend.service.windowsazure.storage.table.entities.dynamic.schema">
  320. <title>Dynamicaly adding properties to Zend_Service_WindowsAzure_Storage_DynamicTableEntity</title>
  321. <programlisting language="php"><![CDATA[
  322. $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity(
  323. 'partition1', '000001'
  324. );
  325. $target->Name = 'Name'; // Will add property "Name" of type "Edm.String"
  326. $target->Age = 25; // Will add property "Age" of type "Edm.Int32"
  327. ]]></programlisting>
  328. </example>
  329. <para>
  330. Optionally, a property type can be enforced:
  331. </para>
  332. <example id="zend.service.windowsazure.storage.table.entities.dynamic.schema-forcedproperties">
  333. <title>Forcing property types on Zend_Service_WindowsAzure_Storage_DynamicTableEntity</title>
  334. <programlisting language="php"><![CDATA[
  335. $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity(
  336. 'partition1', '000001'
  337. );
  338. $target->Name = 'Name'; // Will add property "Name" of type "Edm.String"
  339. $target->Age = 25; // Will add property "Age" of type "Edm.Int32"
  340. // Change type of property "Age" to "Edm.Int32":
  341. $target->setAzurePropertyType('Age', 'Edm.Int64');
  342. ]]></programlisting>
  343. </example>
  344. <para>
  345. The <classname>Zend_Service_WindowsAzure_Storage_Table</classname> class
  346. automatically works with
  347. <classname>Zend_Service_WindowsAzure_Storage_TableEntity</classname> if no specific
  348. class is passed into Table Storage methods.
  349. </para>
  350. </sect3>
  351. <sect3 id="zend.service.windowsazure.storage.table.entities.api">
  352. <title>Entities API examples</title>
  353. <sect4 id="zend.service.windowsazure.storage.table.entities.api.insert">
  354. <title>Inserting an entity</title>
  355. <para>
  356. Using the following code, an entity can be inserted into a table named
  357. "testtable". Note that the table has already been created before.
  358. </para>
  359. <example id="zend.service.windowsazure.storage.table.api.entities.insert.example">
  360. <title>Inserting an entity</title>
  361. <programlisting language="php"><![CDATA[
  362. $entity = new SampleEntity ('partition1', 'row1');
  363. $entity->FullName = "Maarten";
  364. $entity->Age = 25;
  365. $entity->Visible = true;
  366. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  367. 'table.core.windows.net', 'myaccount', 'myauthkey'
  368. );
  369. $result = $storageClient->insertEntity('testtable', $entity);
  370. // Check the timestamp and etag of the newly inserted entity
  371. echo 'Timestamp: ' . $result->getTimestamp() . "\n";
  372. echo 'Etag: ' . $result->getEtag() . "\n";
  373. ]]></programlisting>
  374. </example>
  375. </sect4>
  376. <sect4 id="zend.service.windowsazure.storage.table.entities.api.retrieve-by-id">
  377. <title>Retrieving an entity by partition key and row key</title>
  378. <para>
  379. Using the following code, an entity can be retrieved by partition key and row
  380. key. Note that the table and entity have already been created before.
  381. </para>
  382. <example id="zend.service.windowsazure.storage.table.entities.api.retrieve-by-id.example">
  383. <title>Retrieving an entity by partition key and row key</title>
  384. <programlisting language="php"><![CDATA[
  385. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  386. 'table.core.windows.net', 'myaccount', 'myauthkey'
  387. );
  388. $entity= $storageClient->retrieveEntityById(
  389. 'testtable', 'partition1', 'row1', 'SampleEntity'
  390. );
  391. ]]></programlisting>
  392. </example>
  393. </sect4>
  394. <sect4 id="zend.service.windowsazure.storage.table.entities.api.updating">
  395. <title>Updating an entity</title>
  396. <para>
  397. Using the following code, an entity can be updated. Note that the table and
  398. entity have already been created before.
  399. </para>
  400. <example id="zend.service.windowsazure.storage.table.api.entities.updating.example">
  401. <title>Updating an entity</title>
  402. <programlisting language="php"><![CDATA[
  403. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  404. 'table.core.windows.net', 'myaccount', 'myauthkey'
  405. );
  406. $entity = $storageClient->retrieveEntityById(
  407. 'testtable', 'partition1', 'row1', 'SampleEntity'
  408. );
  409. $entity->Name = 'New name';
  410. $result = $storageClient->updateEntity('testtable', $entity);
  411. ]]></programlisting>
  412. </example>
  413. <para>
  414. If you want to make sure the entity has not been updated before, you can make
  415. sure the <acronym>Etag</acronym> of the entity is checked. If the entity already
  416. has had an update, the update will fail to make sure you do not overwrite any
  417. newer data.
  418. </para>
  419. <example id="zend.service.windowsazure.storage.table.entities.api.updating.example-etag">
  420. <title>Updating an entity (with Etag check)</title>
  421. <programlisting language="php"><![CDATA[
  422. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  423. 'table.core.windows.net', 'myaccount', 'myauthkey'
  424. );
  425. $entity = $storageClient->retrieveEntityById(
  426. 'testtable', 'partition1', 'row1', 'SampleEntity'
  427. );
  428. $entity->Name = 'New name';
  429. // last parameter instructs the Etag check:
  430. $result = $storageClient->updateEntity('testtable', $entity, true);
  431. ]]></programlisting>
  432. </example>
  433. </sect4>
  434. <sect4 id="zend.service.windowsazure.storage.table.entities.api.delete">
  435. <title>Deleting an entity</title>
  436. <para>
  437. Using the following code, an entity can be deleted. Note that the table and
  438. entity have already been created before.
  439. </para>
  440. <example id="zend.service.windowsazure.storage.table.entities.api.delete.example">
  441. <title>Deleting an entity</title>
  442. <programlisting language="php"><![CDATA[
  443. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  444. 'table.core.windows.net', 'myaccount', 'myauthkey'
  445. );
  446. $entity = $storageClient->retrieveEntityById(
  447. 'testtable', 'partition1', 'row1', 'SampleEntity'
  448. );
  449. $result = $storageClient->deleteEntity('testtable', $entity);
  450. ]]></programlisting>
  451. </example>
  452. </sect4>
  453. </sect3>
  454. <sect3 id="zend.service.windowsazure.storage.table.entities.querying">
  455. <title>Performing queries</title>
  456. <para>
  457. Queries in <classname>Zend_Service_WindowsAzure_Storage_Table</classname> table
  458. storage can be performed in two ways:
  459. </para>
  460. <itemizedlist>
  461. <listitem>
  462. <para>
  463. By manually creating a filter condition (involving learning a new query
  464. language)
  465. </para>
  466. </listitem>
  467. <listitem>
  468. <para>
  469. By using the fluent interface provided by the
  470. <classname>Zend_Service_WindowsAzure_Storage_Table</classname>
  471. </para>
  472. </listitem>
  473. </itemizedlist>
  474. <para>
  475. Using the following code, a table can be queried using a filter condition. Note
  476. that the table and entities have already been created before.
  477. </para>
  478. <example id="zend.service.windowsazure.storage.table.entities.querying.query-filter">
  479. <title>Performing queries using a filter condition</title>
  480. <programlisting language="php"><![CDATA[
  481. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  482. 'table.core.windows.net', 'myaccount', 'myauthkey'
  483. );
  484. $entities = $storageClient->storageClient->retrieveEntities(
  485. 'testtable',
  486. 'Name eq \'Maarten\' and PartitionKey eq \'partition1\'',
  487. 'SampleEntity'
  488. );
  489. foreach ($entities as $entity) {
  490. echo 'Name: ' . $entity->Name . "\n";
  491. }
  492. ]]></programlisting>
  493. </example>
  494. <para>
  495. Using the following code, a table can be queried using a fluent interface. Note
  496. that the table and entities have already been created before.
  497. </para>
  498. <example id="zend.service.windowsazure.storage.table.api.entities.query-fluent">
  499. <title>Performing queries using a fluent interface</title>
  500. <programlisting language="php"><![CDATA[
  501. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  502. 'table.core.windows.net', 'myaccount', 'myauthkey'
  503. );
  504. $entities = $storageClient->storageClient->retrieveEntities(
  505. 'testtable',
  506. $storageClient->select()
  507. ->from($tableName)
  508. ->where('Name eq ?', 'Maarten')
  509. ->andWhere('PartitionKey eq ?', 'partition1'),
  510. 'SampleEntity'
  511. );
  512. foreach ($entities as $entity) {
  513. echo 'Name: ' . $entity->Name . "\n";
  514. }
  515. ]]></programlisting>
  516. </example>
  517. </sect3>
  518. <sect3 id="zend.service.windowsazure.storage.table.entities.batch">
  519. <title>Batch operations</title>
  520. <para>
  521. This topic demonstrates how to use the table entity group transaction features
  522. provided by Windows Azure table storage. Windows Azure table storage supports batch
  523. transactions on entities that are in the same table and belong to the same partition
  524. group. A transaction can include at most 100 entities.
  525. </para>
  526. <para>
  527. The following example uses a batch operation (transaction) to insert a set of
  528. entities into the "testtable" table. Note that the table has already been created
  529. before.
  530. </para>
  531. <example id="zend.service.windowsazure.storage.table.api.batch">
  532. <title>Executing a batch operation</title>
  533. <programlisting language="php"><![CDATA[
  534. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  535. 'table.core.windows.net', 'myaccount', 'myauthkey'
  536. );
  537. // Start batch
  538. $batch = $storageClient->startBatch();
  539. // Insert entities in batch
  540. $entities = generateEntities();
  541. foreach ($entities as $entity) {
  542. $storageClient->insertEntity($tableName, $entity);
  543. }
  544. // Commit
  545. $batch->commit();
  546. ]]></programlisting>
  547. </example>
  548. </sect3>
  549. </sect2>
  550. <sect2 id="zend.service.windowsazure.storage.table.sessionhandler">
  551. <title>Table storage session handler</title>
  552. <para>
  553. When running a PHP application on the Windows Azure platform in a load-balanced mode
  554. (running 2 Web Role instances or more), it is important that PHP session data can be
  555. shared between multiple Web Role instances. The Windows Azure SDK for PHP provides the
  556. <classname>Zend_Service_WindowsAzure_SessionHandler</classname> class, which uses
  557. Windows Azure Table Storage as a session handler for PHP applications.
  558. </para>
  559. <para>
  560. To use the <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session
  561. handler, it should be registered as the default session handler for your PHP
  562. application:
  563. </para>
  564. <example id="zend.service.windowsazure.storage.table.api.sessionhandler-register">
  565. <title>Registering table storage session handler</title>
  566. <programlisting language="php"><![CDATA[
  567. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  568. 'table.core.windows.net', 'myaccount', 'myauthkey'
  569. );
  570. $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
  571. $storageClient , 'sessionstable'
  572. );
  573. $sessionHandler->register();
  574. ]]></programlisting>
  575. </example>
  576. <para>
  577. The above classname registers the
  578. <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session handler and will
  579. store sessions in a table called "sessionstable".
  580. </para>
  581. <para>
  582. After registration of the
  583. <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session handler,
  584. sessions can be started and used in the same way as a normal PHP session:
  585. </para>
  586. <example id="zend.service.windowsazure.storage.table.api.sessionhandler-usage">
  587. <title>Using table storage session handler</title>
  588. <programlisting role="php"><![CDATA[
  589. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
  590. 'table.core.windows.net', 'myaccount', 'myauthkey'
  591. );
  592. $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
  593. $storageClient , 'sessionstable'
  594. );
  595. $sessionHandler->register();
  596. session_start();
  597. if (!isset($_SESSION['firstVisit'])) {
  598. $_SESSION['firstVisit'] = time();
  599. }
  600. // ...
  601. ]]></programlisting>
  602. </example>
  603. <warning>
  604. <para>
  605. The <classname>Zend_Service_WindowsAzure_SessionHandler</classname> session handler
  606. should be registered before a call to <functionname>session_start()</functionname>
  607. is made!
  608. </para>
  609. </warning>
  610. </sect2>
  611. </sect1>