0
votes

I've got a azure table storage, with some entities in it. Putting new entities is no problem. Updating existing entities is no problem either, that all works fine.

The following code hangs on the line: $result = $tableClient->retrieveEntities('users',"Name eq 'John Doe'",'partition1'); (somewhere on the bottom) It gives an http 500 error.

require_once("Microsoft/WindowsAzure/Storage/Table.php");
require_once("Microsoft/WindowsAzure/Storage/DynamicTableEntity.php");

$accountName = '***';
$accountKey = '*****';
$tableClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net',$accountName,$accountKey); 
$tableClient->createTableIfNotExists('users');

$result = $tableClient->listTables();
foreach ($result as $table){
    echo 'Table name is: ' . $table->Name . "\r\n<br />";
}
function setUser($accountName,$accountKey,$partitionName,$data) {
    $tableClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net',$accountName,$accountKey);
    $update = false;
    if(isset($data['ID']) && $entity = $tableClient->retrieveEntityById('users',$partitionName,$data['ID'])){
        $update = true;
        unset($data['ID']);
    } else {
        $guid = substr(com_create_guid(),1,32);
        $entity = new Microsoft_WindowsAzure_Storage_DynamicTableEntity($partitionName, $guid);
        $entity->ID = $guid;
    }

    $keys = array_keys($data);
    foreach($keys as $key){
        $entity->$key = $data[$key];
    }       
    if($update){
        $tableClient->updateEntity('users',$entity,true);
    } else {
        $tableClient->insertEntity('users',$entity);
    }
}   
setUser($accountName,$accountKey,'partition1',array(Name => 'John Doe',Email => '[email protected]',Time => time()));



$result = $tableClient->retrieveEntities('users',"Name eq 'John Doe'",'partition1');
//$result = $tableClient->retrieveEntities('users');




foreach($result as $data) {
    echo $data->ID;
    echo ' - ';
    echo $data->Name;
    echo ' - ';
    echo $data->Email;
    echo ' - ';
    echo $data->Time;
    echo '<br />';
}

What I want to know is;

  1. What am I doing wrong with the filter paramater? (When I comment out the line with the filter and uncomment the line below, it works fine).

  2. How do I catch this error(and other similar errors), I tried a try { } catch { }, but that wasn't working. It resulted in the same HTTP 500 error.

try-catch - code:

try {
    $result = $tableClient->retrieveEntities('users',"Name eq 'John Doe'",'partition1');
} catch (Microsoft_WindowsAzure_Exception $e) {
    echo 'Caught exception: '.$e->getMessage();
}
1
Kind of unrelated comment, but is there a particular reason you're not using Official PHP SDK from Microsoft: github.com/Azure/azure-sdk-for-php?Gaurav Mantri
I am using an Official PHP SDK from Microsoft, but I think it's an older version? I used it for the normal blob storage before, which worked fine back then. I can try the SDK you are pointing to, who knows this solves my problem but than I'm back to 0. It would be nice if someone sees what I'm doing wrong.Jeroen
Just taking a wild guess (so I may be wrong) - I looked at the code for retrieving entities here: github.com/Daniel15/AjaXplorer-Azure/blob/master/azuresdk/… (Hope I am looking at right place) and noticed that you're passing partition1 as one of the arguments to the method. Could that be causing the problem. Try something like $tableClient->retrieveEntities('users',"PartitionKey eq 'partition1' and Name eq 'John Doe'") and see if that fixes the problem.Gaurav Mantri
Thank you, that's solving the filter problem! The partition apparently is not an argument of the "retrieveEntities" method. If there is no result I just get back nothing, so if the count(array) is 0, the entity requested is not existing. Try-catch isn't needed anymore!Jeroen
@GuaravMantri. Can you post your comment as an answer so I can mark it "solved". Thank you.Jeroen

1 Answers

1
votes

Just taking a wild guess (so I may be wrong) - I looked at the code for retrieving entities here: https://github.com/Daniel15/AjaXplorer-Azure/blob/master/azuresdk/Microsoft/WindowsAzure/Storage/Table.php (Hope I am looking at right place) and noticed that you're passing partition1 as one of the arguments to the method.

Could that be causing the problem? Try something like:

$tableClient->retrieveEntities('users',"PartitionKey eq 'partition1' and Name eq 'John Doe'") 

I also looked at the project's page on CodePlex (http://phpazure.codeplex.com/) and it is mentioned that this project has been deprecated in favor of Azure SDK on Github (https://github.com/Azure/azure-sdk-for-php). IMHO, It may be worthwhile investing some time in porting your application to use the new SDK. Just a thought.