Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

About the author

Maarten Balliauw is currently employed as a Technical Evangelist at JetBrains. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Pro NuGet Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
Maarten Balliauw - MVP - Most Valuable Professional
Maarten Balliauw - ASPInsider

Search

Archive

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright Maarten Balliauw 2013


PHP SDK for Windows Azure - Milestone 2 release

WindowsAzure I’m proud to announce our second milestone for the PHP SDK for Windows Azure project that Microsoft and RealDolmen started back in May. Next to our regular releases on CodePlex, we’ll also be shipping a Zend Framework version of the PHP SDK for Windows Azure. Announcements on this will be made later.

The current milestone is focused on Windows Azure Table Storage, enabling you to use all features this service offers from any PHP application, be it hosted in-premise or on Windows Azure.

Get it while it’s hot: PHP SDK for Windows Azure CTP2 - PHPAzure CTP2 (0.2.0)

Detailed API documentation is provided in the download package, while more descriptive guidance is available on the project site.

Working with Azure Table Storage from PHP

Let’s provide a small example on the new Table Storage support in the PHP SDK for Windows Azure. The first thing to do when you have a clean storage account on the Azure platform is to create a new table:

/** Microsoft_Azure_Storage_Table */
require_once 'Microsoft/Azure/Storage/Table.php';

$storageClient = new Microsoft_Azure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');
$storageClient->createTable('mynewtable');

Easy, no? Note that we did not provide any schema information here as you would do in a regular database. Windows Azure Table Storage can actually contain entities with different properties in the same table. You can work with an enforced schema, but this will be client-side. More info on that matter is available here.

Now let’s add a person to the “mynewtable” in the cloud:

$person = new Microsoft_Azure_Storage_DynamicTableEntity('partition1', 'row1');
$person->Name = "Maarten";
$person->Age  = 25;

$storageClient->insertEntity('mynewtable', $person);

Again, no rocket science. The Microsoft_Azure_Storage_DynamicTableEntity class used provides fluent access to entities in Table Storage. More info on this class is available here.

Now let’s add a property to this $person instance and merge it into Table Storage:

$person->Blog = "www.maartenballiauw.be";
$storageClient->mergeEntity('mynewtable', $person);

Wow! We just added a Blog property to this object! I could have also used updateEnity for this, but that one would have overwritten eventual changes that were made to my $person in the meantime.

Now for some querying. Let’s retrieve all entities in “mynewtable” that have an Age of 25:

$entities = $storageClient->storageClient->retrieveEntities(
    'mynewtable',
    $storageClient->select()
                  ->from($tableName)
                  ->where('Age eq ?', 25)
);

foreach ($entities as $entity)
{
    echo 'Name: ' . $entity->Name . "\n";
}

I guess this al looks quite straightforward. The fluent query building API provides a syntax similar to how you would build a query in SQL.

Another nice feature of the PHP SDK for Windows Azure is support for batch transactions. Here’s an example of how to work with transactions on Table Storage:

// Start batch
$batch = $storageClient->startBatch();

// Insert entities in batch
$entities = array( ...... );
foreach ($entities as $entity)
{
    $storageClient->insertEntity('mynewtable', $entity);
}

// Commit
$batch->commit();

The batch will fail as a whole if one insert, update, delete, ... does not work out, just like with a transaction on a regular relational database like MySQL or SQL Server.

If you're interested in cloud computing and WIndows Azure, and want to keep using PHP, make sure to get the latest version of the PHP SDK for Windows Azure to leverage all functionality that is available in the cloud. Here's the link: PHP SDK for Windows Azure CTP2 - PHPAzure CTP2 (0.2.0)


Pingbacks and trackbacks (1)+

Comments are closed