Getting Started

Back to Getting Started.
Back to Table storage.

Defining entities for Table Storage

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 name, 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.

Windows Azure SDK for PHP provides 2 ways of working with entities:
  • Enforced schema
  • No enforced schema

Enforced schema entities

To enforce a schema on the client side using Windows Azure SDK for PHP, you can create a class which inherits Microsoft_WindowsAzure_Storage_TableEntity. This class provides some basic functionality for the Windows Azure SDK for PHP to work with a client-side schema.

Base properties provided by Microsoft_WindowsAzure_Storage_TableEntity are:
  • PartitionKey (exposed trough getPartitionKey() and setPartitionKey())
  • RowKey (exposed trough getRowKey() and setRowKey())
  • Timestamp (exposed trough getTimestamp() and setTimestamp())
  • Etag value (exposed trough getEtag() and setEtag())

Here's a sample class inheriting Microsoft_WindowsAzure_Storage_TableEntity:
class SampleEntity extends Microsoft_WindowsAzure_Storage_TableEntity
{
    /**
     * @azure Name
     */
    public $Name;
    
    /**
     * @azure Age Edm.Int64
     */
    public $Age;
    
    /**
     * @azure Visible Edm.Boolean
     */
    public $Visible = false;
}

Defining properties

The Windows Azure SDK for PHP will map any class inherited from Microsoft_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:
/**
 * @azure <property name in Windows Azure> <optional property type>
 */
public $<property name in PHP>;

Let's see how to define a propety "Age" as an integer on Windows Azure table storage:
/**
 * @azure Age Edm.Int64
 */
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. This will be mapped to a string in PHP.
  • Edm.Boolean - A boolean value. This will be mapped to a boolean in PHP.
  • 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. This will be mapped to a DateTime objectin PHP.
  • Edm.Double - A 64-bit floating point value. This will be mapped to a double in PHP.
  • Edm.Guid - A 128-bit globally unique identifier. This will be mapped to a string in PHP.
  • Edm.Int32 - A 32-bit integer. This will be mapped to an integer in PHP.
  • Edm.Int64 - A 64-bit integer. This will be mapped to an integer in PHP.
  • Edm.String - A UTF-16-encoded value. String values may be up to 64 KB in size. This will be mapped to a string in PHP.

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

To use Windows Azure SDK for PHP without defining a schema, you can make use of the Microsoft_WindowsAzure_Storage_DynamicTableEntity class. This class inherits Microsoft_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 Microsoft_WindowsAzure_Storage_DynamicTableEntity are:
  • PartitionKey (exposed trough getPartitionKey() and setPartitionKey())
  • RowKey (exposed trough getRowKey() and setRowKey())
  • Timestamp (exposed trough getTimestamp() and setTimestamp())
  • Etag value (exposed trough getEtag() and setEtag())

Other properties can be added on the fly. Their Windows Azure table storage type will be determined on-the-fly:
$target = new Microsoft_WindowsAzure_Storage_DynamicTableEntity('partition1', '000001');
$target->Name = 'Name'; // Will add property "Name" of type "Edm.String"
$target->Age  = 25; // Will add property "Age" of type "Edm.Int32"

Optionally, a property type can be enforced:
$target = new Microsoft_WindowsAzure_Storage_DynamicTableEntity('partition1', '000001');
$target->Name = 'Name'; // Will add property "Name" of type "Edm.String"
$target->Age  = 25; // Will add property "Age" of type "Edm.Int32"

$target->setAzurePropertyType('Age', 'Edm.Int64'); // Changes type of property "Age" to "Edm.Int32"

Windows Azure SDK for PHP automatically works with Microsoft_WindowsAzure_Storage_DynamicTableEntity if no specific class is passed into Table Storage methods.

Last edited Apr 1, 2011 at 8:14 AM by maartenba, version 10

Comments

No comments yet.