DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > Sorting Azure Table Entities by Timestamp with PHP

Sorting Azure Table Entities by Timestamp with PHP

Brian Swan user avatar by
Brian Swan
·
Apr. 26, 11 · Cloud Zone · News
Like (0)
Save
Tweet
5.64K Views

Join the DZone community and get the full member experience.

Join For Free

This is a short post that describes one way to sort Windows Azure Table entities by timestamp when using the Windows Azure SDK for PHP. The problem boils down to sorting an array of objects by a timestamp property, so the information here is nothing that hasn’t been done before. However, after spending some time looking for a way to use a filter in the Windows Azure SDK for PHP API, I didn’t find one, so I’m hoping this post might save others some time. In the end, my solution simply uses the PHP array_multisort function.

To illustrate the problem and my solution, I’ll reuse some of the code I wrote in this post: Accessing Windows Azure Table Storage from PHP. The important piece in that post to keep in mind is that when you define a class that inherits from Microsoft_WindowsAzure_Storage_TableEntity, the Windows Azure SDK for PHP maps specially annotated properties to “columns” in a table that has the same name as the class name. So, the example class below maps to the Contacts table and the $Name, $Address, and $Phone properties map to “columns” in that table.

class Contact extends Microsoft_WindowsAzure_Storage_TableEntity
{
  /**
  * @azure Name
  */
  public $Name;


  /**
  * @azure Address
  */
  public $Address;


  /**
  * @azure Phone
  */
  public $Phone;
}

Also note that the Contact class inherits the $_partitionKey, $_rowKey, and $_timestamp properties, as well as the corresponding methods getPartitionKey(), getRowKey(), and getTimestamp().

With that class in place, I can execute a query like this to retrieve all contacts:

$tableStorageClient = new Microsoft_WindowsAzure_Storage_Table();


$contacts = $tableStorageClient->retrieveEntities("Contact", null, "Contact");
  • The first parameter in the call to retrieveEntities specifies the table from which entities are retrieved.
  • The second parameter is a filter for entities. Specifying null will retrieve all entities.
  • The last parameter specifies the object type entities are retrieved as.

To sort the entities by timestamp, I’ll use two functions (which I essentially copied from the comments in the array_multisort documentation):

function objSort(&$objArray,$indexFunction,$sort_flags=0)
{
     $indices = array();
     foreach($objArray as $obj)
     {
         $indeces[] = $indexFunction($obj);
     }
     return array_multisort($indeces,$objArray,$sort_flags);
}


function getIndex($obj)
{
     return $obj->getTimestamp();
}

Note: If you are using the Windows Azure SDK v3.0.0 or later, timestamps are returned as PHP DateTime objects. So, to for this sorting solution to work, you’ll need to alter the getIndex function slightly (to get the Timestamp property on a DateTime object):

function getIndex($obj)
{ 
      return $obj->getTimestamp()->getTimestamp();
}

Now I can sort the $contacts array like this:

objSort($messages,'getIndex');

That’s it! I love the comments in the PHP documentation. :-)

PHP Database azure Sorting

Published at DZone with permission of Brian Swan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Most Popular Kubernetes Alternatives and Competitors
  • Is Java Still Relevant?
  • How to Submit a Post to DZone
  • Password Authentication: How to Correctly Do It

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo