Getting Started
Back to
Getting Started.
Table storage
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 Windows Azure SDK for PHP in order to provide a native PHP interface to the storage account.
API examples
Table Storage API can be divided into 3 parts:
- Operations on tables
- Operations on entities
- Batch operations
This topic lists some examples of using the PHPAzure SDK. Other features are available in the download package, as well as a detailed API documentation of those features.
Note: 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:
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$result = $storageClient->createTable('testtable');
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:
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$result = $storageClient->listTables();
foreach ($result as $table)
{
echo 'Table name is: ' . $table->Name . "\r\n";
}
Operations on entities
This topic lists some samples of operations that can be executed on entities. It assumes you have read
Defining entities for Table Storage.
All examples will make use of the following enforced schema class:
class SampleEntity extends Microsoft_WindowsAzure_Storage_TableEntity
{
/**
* @azure Name
*/
public $Name;
/**
* @azure Age Edm.Int64
*/
public $Age;
/**
* @azure Visible Edm.Boolean
*/
public $Visible = false;
}
Note that if no schema class is passed into table storage methods, Windows Azure SDK for PHP automatically works with Microsoft_WindowsAzure_Storage_DynamicTableEntity.
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.
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$entity = new SampleEntity ('partition1', 'row1');
$entity->FullName = "Maarten";
$entity->Age = 25;
$entity->Visible = true;
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$result = $storageClient->insertEntity('testtable', $entity);
// Check the timestamp and etag of the newly inserted entity
echo 'Timestamp: ' . $result->getTimestamp() . "\n";
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.
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$entity= $storageClient->retrieveEntityById('testtable', 'partition1', 'row1', 'SampleEntity');
Updating an entity
Using the following code, an entity can be updated. Note that the table and entity have already been created before.
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$entity = $storageClient->retrieveEntityById('testtable', 'partition1', 'row1', 'SampleEntity');
$entity->Name = 'New name';
$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:
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$entity = $storageClient->retrieveEntityById('testtable', 'partition1', 'row1', 'SampleEntity');
$entity->Name = 'New name';
$result = $storageClient->updateEntity('testtable', $entity, true); // last parameter instructs the Etag check
Deleting an entity
Using the following code, an entity can be deleted. Note that the table and entity have already been created before.
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$entity = $storageClient->retrieveEntityById('testtable', 'partition1', 'row1', 'SampleEntity');
$result = $storageClient->deleteEntity('testtable', $entity);
Performing queries
Queries in Windows Azure SDK for PHP 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 Windows Azure SDK for PHP
Performing queries using a filter condition
Using the following code, a table can be queried using a filter condition. Note that the table and entities have already been created before.
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$entities = $storageClient->storageClient->retrieveEntities(
'testtable',
'Name eq \'Maarten\' and PartitionKey eq \'partition1\'',
'SampleEntity'
);
foreach ($entities as $entity)
{
echo 'Name: ' . $entity->Name . "\n";
}
More filter options can be found on the
corresponding MSDN page.
Performing queries using a fluent interface
Using the following code, a table can be queried using a fluent interface. Note that the table and entities have already been created before.
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$entities = $storageClient->storageClient->retrieveEntities(
'testtable',
$storageClient->select()
->from($tableName)
->where('Name eq ?', 'Maarten')
->andWhere('PartitionKey eq ?', 'partition1'),
'SampleEntity'
);
foreach ($entities as $entity)
{
echo 'Name: ' . $entity->Name . "\n";
}
More filter options can be found on the
corresponding MSDN page.
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.
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';
$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
// Start batch
$batch = $storageClient->startBatch();
// Insert entities in batch
$entities = generateEntities();
foreach ($entities as $entity)
{
$storageClient->insertEntity($tableName, $entity);
}
// Commit
$batch->commit();