Zend_Service_WindowsAzure_Storage_Queue | Zend_Service Programmer's Reference Guide | Zend_Service_Yahoo

# Zend_Service_WindowsAzure_Storage_Table

The Table service offers structured storage in the form of tables.

Table Storage is offered by Windows Azure as a REST API which is wrapped by the Zend_Service_WindowsAzure_Storage_Table class in order to provide a native PHP interface to the storage account.

This topic lists some examples of using the Zend_Service_WindowsAzure_Storage_Table class. Other features are available in the download package, as well as a detailed API documentation of those features.

Note that development table storage (in the Windows Azure SDK) does not support all features provided by the API. Therefore, the examples listed on this page are to be used on Windows Azure production table storage.

# Operations on tables

This topic lists some samples of operations that can be executed on tables.

# Creating a table

Using the following code, a table can be created on Windows Azure production table storage.

Example #1 Creating a table

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $result = $storageClient->createTable('testtable');
6. echo 'New table name is: ' . $result->Name;

# Listing all tables

Using the following code, a list of all tables in Windows Azure production table storage can be queried.

Example #2 Listing all tables

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $result = $storageClient->listTables();
5. foreach ($result as $table) {
6. echo 'Table name is: ' . $table->Name . "\r\n";
7. }

# Operations on entities

Tables store data as collections of entities. Entities are similar to rows. An entity has a primary key and a set of properties. A property is a named, typed-value pair, similar to a column.

The Table service does not enforce any schema for tables, so two entities in the same table may have different sets of properties. Developers may choose to enforce a schema on the client side. A table may contain any number of entities.

Zend_Service_WindowsAzure_Storage_Table provides 2 ways of working with entities:

- Enforced schema

- No enforced schema

All examples will make use of the following enforced schema class.

Example #3 Enforced schema used in samples

1. class SampleEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
2. {
3. /**
4. * @azure Name
5. */
6. public $Name;
8. /**
9. * @azure Age Edm.Int64
10. */
11. public $Age;
13. /**
14. * @azure Visible Edm.Boolean
15. */
16. public $Visible = false;
17. }

Note that if no schema class is passed into table storage methods, Zend_Service_WindowsAzure_Storage_Table automatically works with Zend_Service_WindowsAzure_Storage_DynamicTableEntity.

# Enforced schema entities

To enforce a schema on the client side using the Zend_Service_WindowsAzure_Storage_Table class, you can create a class which inherits Zend_Service_WindowsAzure_Storage_TableEntity. This class provides some basic functionality for the Zend_Service_WindowsAzure_Storage_Table class to work with a client-side schema.

Base properties provided by Zend_Service_WindowsAzure_Storage_TableEntity are:

- PartitionKey (exposed through getPartitionKey() and setPartitionKey())

- RowKey (exposed through getRowKey() and setRowKey())

- Timestamp (exposed through getTimestamp() and setTimestamp())

- Etag value (exposed through getEtag() and setEtag())

Here's a sample class inheriting Zend_Service_WindowsAzure_Storage_TableEntity:

Example #4 Sample enforced schema class

1. class SampleEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
2. {
3. /**
4. * @azure Name
5. */
6. public $Name;
8. /**
9. * @azure Age Edm.Int64
10. */
11. public $Age;
13. /**
14. * @azure Visible Edm.Boolean
15. */
16. public $Visible = false;
17. }

The Zend_Service_WindowsAzure_Storage_Table class will map any class inherited from Zend_Service_WindowsAzure_Storage_TableEntity to Windows Azure table storage entities with the correct data type and property name. All there is to storing a property in Windows Azure is adding a docblock comment to a public property or public getter/setter, in the following format:

Example #5 Enforced property

1. /**
2. * @azure <property name in Windows Azure> <optional property type>
3. */
4. public $<property name in PHP>;

Let's see how to define a propety "Age" as an integer on Windows Azure table storage:

Example #6 Sample enforced property

1. /**
2. * @azure Age Edm.Int64
3. */
4. public $Age;

Note that a property does not necessarily have to be named the same on Windows Azure table storage. The Windows Azure table storage property name can be defined as well as the type.

The following data types are supported:

- `Edm.Binary` - An array of bytes up to 64 KB in size.

- `Edm.Boolean` - A boolean value.

- `Edm.DateTime` - A 64-bit value expressed as Coordinated Universal Time (UTC). The supported DateTime range begins from 12:00 midnight, January 1, 1601 A.D. (C.E.), Coordinated Universal Time (UTC). The range ends at December 31st, 9999.

- `Edm.Double` - A 64-bit floating point value.

- `Edm.Guid` - A 128-bit globally unique identifier.

- `Edm.Int32` - A 32-bit integer.

- `Edm.Int64` - A 64-bit integer.

- `Edm.String` - A UTF-16-encoded value. String values may be up to 64 KB in size.

# No enforced schema entities (a.k.a. DynamicEntity)

To use the Zend_Service_WindowsAzure_Storage_Table class without defining a schema, you can make use of the Zend_Service_WindowsAzure_Storage_DynamicTableEntity class. This class inherits Zend_Service_WindowsAzure_Storage_TableEntity like an enforced schema class does, but contains additional logic to make it dynamic and not bound to a schema.

Base properties provided by Zend_Service_WindowsAzure_Storage_DynamicTableEntity are:

- PartitionKey (exposed through getPartitionKey() and setPartitionKey())

- RowKey (exposed through getRowKey() and setRowKey())

- Timestamp (exposed through getTimestamp() and setTimestamp())

- Etag value (exposed through getEtag() and setEtag())

Other properties can be added on the fly. Their Windows Azure table storage type will be determined on-the-fly:

Example #7 Dynamicaly adding properties to Zend_Service_WindowsAzure_Storage_DynamicTableEntity

1. $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity(
2. 'partition1', '000001'
3. );
4. $target->Name = 'Name'; // Will add property "Name" of type "Edm.String"
5. $target->Age = 25; // Will add property "Age" of type "Edm.Int32"

Optionally, a property type can be enforced:

Example #8 Forcing property types on Zend_Service_WindowsAzure_Storage_DynamicTableEntity

1. $target = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity(
2. 'partition1', '000001'
3. );
4. $target->Name = 'Name'; // Will add property "Name" of type "Edm.String"
5. $target->Age = 25; // Will add property "Age" of type "Edm.Int32"
7. // Change type of property "Age" to "Edm.Int32":
8. $target->setAzurePropertyType('Age', 'Edm.Int64');

The Zend_Service_WindowsAzure_Storage_Table class automatically works with Zend_Service_WindowsAzure_Storage_TableEntity if no specific class is passed into Table Storage methods.

# Entities API examples

# Inserting an entity

Using the following code, an entity can be inserted into a table named "testtable". Note that the table has already been created before.

Example #9 Inserting an entity

1. $entity = new SampleEntity ('partition1', 'row1');
2. $entity->FullName = "Maarten";
3. $entity->Age = 25;
4. $entity->Visible = true;
6. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
7. 'table.core.windows.net', 'myaccount', 'myauthkey'
8. );
9. $result = $storageClient->insertEntity('testtable', $entity);
11. // Check the timestamp and etag of the newly inserted entity
12. echo 'Timestamp: ' . $result->getTimestamp() . "\n";
13. echo 'Etag: ' . $result->getEtag() . "\n";

# Retrieving an entity by partition key and row key

Using the following code, an entity can be retrieved by partition key and row key. Note that the table and entity have already been created before.

Example #10 Retrieving an entity by partition key and row key

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $entity= $storageClient->retrieveEntityById(
5. 'testtable', 'partition1', 'row1', 'SampleEntity'
6. );

# Updating an entity

Using the following code, an entity can be updated. Note that the table and entity have already been created before.

Example #11 Updating an entity

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $entity = $storageClient->retrieveEntityById(
5. 'testtable', 'partition1', 'row1', 'SampleEntity'
6. );
8. $entity->Name = 'New name';
9. $result = $storageClient->updateEntity('testtable', $entity);

If you want to make sure the entity has not been updated before, you can make sure the Etag of the entity is checked. If the entity already has had an update, the update will fail to make sure you do not overwrite any newer data.

Example #12 Updating an entity (with Etag check)

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $entity = $storageClient->retrieveEntityById(
5. 'testtable', 'partition1', 'row1', 'SampleEntity'
6. );
8. $entity->Name = 'New name';
10. // last parameter instructs the Etag check:
11. $result = $storageClient->updateEntity('testtable', $entity, true);

# Deleting an entity

Using the following code, an entity can be deleted. Note that the table and entity have already been created before.

Example #13 Deleting an entity

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $entity = $storageClient->retrieveEntityById(
5. 'testtable', 'partition1', 'row1', 'SampleEntity'
6. );
7. $result = $storageClient->deleteEntity('testtable', $entity);

# Performing queries

Queries in Zend_Service_WindowsAzure_Storage_Table table storage can be performed in two ways:

- By manually creating a filter condition (involving learning a new query language)

- By using the fluent interface provided by the Zend_Service_WindowsAzure_Storage_Table

Using the following code, a table can be queried using a filter condition. Note that the table and entities have already been created before.

Example #14 Performing queries using a filter condition

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $entities = $storageClient->storageClient->retrieveEntities(
5. 'testtable',
6. 'Name eq \'Maarten\' and PartitionKey eq \'partition1\'',
7. 'SampleEntity'
8. );
10. foreach ($entities as $entity) {
11. echo 'Name: ' . $entity->Name . "\n";
12. }

Using the following code, a table can be queried using a fluent interface. Note that the table and entities have already been created before.

Example #15 Performing queries using a fluent interface

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
4. $entities = $storageClient->storageClient->retrieveEntities(
5. 'testtable',
6. $storageClient->select()
7. ->from($tableName)
8. ->where('Name eq ?', 'Maarten')
9. ->andWhere('PartitionKey eq ?', 'partition1'),
10. 'SampleEntity'
11. );
13. foreach ($entities as $entity) {
14. echo 'Name: ' . $entity->Name . "\n";
15. }

# Batch operations

This topic demonstrates how to use the table entity group transaction features provided by Windows Azure table storage. Windows Azure table storage supports batch transactions on entities that are in the same table and belong to the same partition group. A transaction can include at most 100 entities.

The following example uses a batch operation (transaction) to insert a set of entities into the "testtable" table. Note that the table has already been created before.

Example #16 Executing a batch operation

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
5. // Start batch
6. $batch = $storageClient->startBatch();
8. // Insert entities in batch
9. $entities = generateEntities();
10. foreach ($entities as $entity) {
11. $storageClient->insertEntity($tableName, $entity);
12. }
14. // Commit
15. $batch->commit();

# Table storage session handler

When running a PHP application on the Windows Azure platform in a load-balanced mode (running 2 Web Role instances or more), it is important that PHP session data can be shared between multiple Web Role instances. The Windows Azure SDK for PHP provides the Zend_Service_WindowsAzure_SessionHandler class, which uses Windows Azure Table Storage as a session handler for PHP applications.

To use the Zend_Service_WindowsAzure_SessionHandler session handler, it should be registered as the default session handler for your PHP application:

Example #17 Registering table storage session handler

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
5. $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
6. $storageClient , 'sessionstable'
7. );
8. $sessionHandler->register();

The above classname registers the Zend_Service_WindowsAzure_SessionHandler session handler and will store sessions in a table called "sessionstable".

After registration of the Zend_Service_WindowsAzure_SessionHandler session handler, sessions can be started and used in the same way as a normal PHP session:

Example #18 Using table storage session handler

1. $storageClient = new Zend_Service_WindowsAzure_Storage_Table(
2. 'table.core.windows.net', 'myaccount', 'myauthkey'
3. );
5. $sessionHandler = new Zend_Service_WindowsAzure_SessionHandler(
6. $storageClient , 'sessionstable'
7. );
8. $sessionHandler->register();
10. session_start ();
12. if (!isset ($_SESSION['firstVisit'])) {
13. $_SESSION['firstVisit'] = time ();
14. }
16. // ...

Warning

The Zend_Service_WindowsAzure_SessionHandler session handler should be registered before a call to session_start() is made!

Zend_Service_WindowsAzure_Storage_Queue | Zend_Service Programmer's Reference Guide | Zend_Service_Yahoo