【问题标题】:Azuretable storage PHP if entity Properties Not defined Results in ErrorAzuretable 存储 PHP 如果实体属性未定义导致错误
【发布时间】:2013-12-17 03:13:48
【问题描述】:

我将数据存储在 Azure 表存储中。对于每个客户,我都有一些属性(如姓名、电子邮件、地址属性)。对于某些客户,我们没有这些属性。 (稍后在旅途中添加)

当我尝试通过 PHP 提取客户实体属性时,如果未定义该属性,则会引发致命错误。

例如,在下面的代码中,我有附加属性实体属性,它只为少数客户定义。并非所有客户都拥有该实体。如果我使用下面的代码,它会引发致命错误。

如何处理这个异常。

$tableRestProxy = ServicesBuilder::getInstance()->createTableService($this->connectionString);
$filter = "PartitionKey eq '" . $this->apiusr . "' and RowKey eq '" . $this->apiusr . "' and apiseceret eq '" . $this->apipass . "'";

$entities = $result->getEntities();
if (count($entities) > 0 ) 
{
$this->user_valid = true;
$this->cid = $entity->getProperty("cid")->getValue();
**$this->additonal_attribute = $entity->getProperty("additiona_attributes")->getValue()**; <--------------
}

既然这不是 SQL 数据库,那么 PHP 中的解决方案是什么? (每行的架构可以不同)。如何在 PHP 中处理?

我使用的代码来自 http://www.windowsazure.com/en-us/develop/php/how-to-guides/table-service/

【问题讨论】:

    标签: php azure azure-blob-storage azure-table-storage


    【解决方案1】:

    没有使用过 PHP(所以我可能错了),但您是否会因为试图获取 null 对象的值而收到此错误?当您在没有此属性的实体上调用 getProperty() 时,它会返回 null,然后您在该 null 对象上调用 getValue()

    我查找了PHP SDK on GitHub 的源代码,发现了另一种可以尝试的方法。那个方法是getPropertyValue($name)。如果在实体中找不到该属性,则使用此属性,您将返回 null 值。所以你的代码看起来像:

    $this->additonal_attribute = $entity->getPropertyValue("additiona_attributes")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 2012-04-03
      • 2012-08-16
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多