2017-03-14 49 views
0

我目前正在爲亞馬遜的echo點開發一項技術,它需要使用持久數據。我在爲我的技能開發Web界面時遇到了一個問題,因爲我無法輕鬆更新該技能使用的DynamoDB表的mapAttr列。無法更新DynamoDB表的地圖列

過去兩天我一直在努力解決這個問題,包括文檔在內的任何地方都找遍了,但似乎找不到任何能幫助我的東西。

這是我使用的代碼:

 $result = $client->updateItem([ 
      'TableName' => 'rememberThisDBNemo', 
      'Key' => [ 
       'userId' => [ 'S' => $_SESSION['userDataAsk'] ] 
      ], 
      'ExpressionAttributeNames' => [ 
       '#attr' => 'mapAttr.ReminderJSON' 
      ], 
      'ExpressionAttributeValues' => [ 
       ':val1' => json_encode($value) 
      ], 
      'UpdateExpression' => 'SET #attr = :val1' 
     ]); 

我已經嘗試了許多不同的事情,所以這可能只是絕對錯誤的,但沒有,我已經找到工作過。

該表具有2欄用戶idmapAttr,用戶id是一個字符串和mapAttr是地圖。最初我以爲它只是一個JSON字符串,但它不是那樣的,因爲當我試圖用JSON字符串直接更新它時,它會在被Alexa讀取時停止工作。

我只是試圖更新mapAttr的2個屬性中的1個。這是一個字符串ReminderJSON。

任何幫助,將不勝感激。謝謝。

回答

0

嘗試調用的updateItem這樣

$result = $client->updateItem([ 
     'TableName' => 'rememberThisDBNemo', 
     'Key' => [ 
      'userId' => [ 'S' => $_SESSION['userDataAsk'] ] 
     ], 
     'ExpressionAttributeNames' => [ 
      '#mapAttr' => 'mapAttr', 
      '#attr' => 'ReminderJSON' 
     ], 
     'ExpressionAttributeValues' => [ 
      ':val1' => ['S' => json_encode($value)] 
     ], 
     'UpdateExpression' => 'SET #mapAttr.#attr = :val1' 
    ]); 

但是,請注意,爲了使這項工作,屬性mapAttr必須已經存在。如果沒有,你會得到ValidationException說The document path provided in the update expression is invalid for update...

作爲一種變通方法,您可能需要一個ConditionExpression => 'attribute_exists(mapAttr)'添加到您的參數,可以捕獲可能的異常,然後執行另一次更新增加新的屬性mapAttr:

try { 
    $result = $client->updateItem([ 
     'TableName' => 'rememberThisDBNemo', 
     'Key' => [ 
      'userId' => [ 'S' => $_SESSION['userDataAsk'] ] 
     ], 
     'ExpressionAttributeNames' => [ 
      '#mapAttr' => 'mapAttr' 
      '#attr' => 'ReminderJSON' 
     ], 
     'ExpressionAttributeValues' => [ 
      ':val1' => ['S' => json_encode($value)] 
     ], 
     'UpdateExpression' => 'SET #mapAttr.#attr = :val1' 
     'ConditionExpression' => 'attribute_exists(#mapAttr)' 
    ]); 

    } catch (\Aws\Exception\AwsException $e) { 
    if ($e->getAwsErrorCode() == "ConditionalCheckFailedException") { 
     $result = $client->updateItem([ 
      'TableName' => 'rememberThisDBNemo', 
      'Key' => [ 
       'userId' => [ 'S' => $_SESSION['userDataAsk'] ] 
      ], 
      'ExpressionAttributeNames' => [ 
       '#mapAttr' => 'mapAttr' 
      ], 
      'ExpressionAttributeValues' => [ 
       ':mapValue' => ['M' => ['ReminderJSON' => ['S' => json_encode($value)]]] 
      ], 
      'UpdateExpression' => 'SET #mapAttr = :mapValue' 
      'ConditionExpression' => 'attribute_not_exists(#mapAttr)' 
     ]); 
    } 
    } 
+0

我嘗試了兩種方法,但他們都沒有工作。儘管我的許多嘗試都有一個常見的奇怪事情:*許多響應通常是空白的,但返回一個標準的200 HTTP響應代碼*,我嘗試使用'AwsException'和'DynamoDbException'來捕獲異常,但是我不能抓住任何東西 – CristianHG

+0

難道你只是沒有注意到,而不是更新mapAttr map中的ReminderJSON項目,而是創建了一個新的頂級屬性'mapAttr.ReminderJSON'(是的,其名稱中帶有一個點)。因爲如果你指定''#attr'=>'mapAttr.ReminderJSON',就像你在代碼片段中顯示的那樣。請注意,在我的示例中,映射和屬性名稱的指定方式不同。 – xtx