DynamoDb - Integer can not be converted to an String

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I'm new to dynamoDB and am struggling to populate my DynamoDB table which I have defined the table as follows:

    $response = $DynamoDb->createTable([         'TableName' => 'products',         'AttributeDefinitions' => [             [                 'AttributeName' => 'product_code',                 'AttributeType' => 'N'             ],             [                 'AttributeName' => 'created_at',                 'AttributeType' => 'N'             ],         ],         'KeySchema' => [             [                 'AttributeName' => 'product_code',                 'KeyType' => 'HASH'              ],             [                 'AttributeName' => 'created_at',                 'KeyType' => 'RANGE'              ]         ],         'ProvisionedThroughput' => [             'ReadCapacityUnits'    => 5,             'WriteCapacityUnits' => 6         ]     ]); 

and populated using

    $result = $DynamoDb->putItem([       'TableName' => 'products',         'Item' => [           'product_code' => ['N'  =>  (int)$row[0]],           'created_at' => ['N' => (int)strtotime("now")]         ]     ]); 

I keep getting the error

Integer can not be converted to an String 

Can anybody please advise a newbie. Many thanks.

回答1:

you should insert to dynamo str values. dynamo will convert it to integer if 'N' is the type of the row

"N": "string",    $result = $DynamoDb->putItem([           'TableName' => 'products',             'Item' => [               'product_code' => ['N'  =>  (str)$row[0]],               'created_at' => ['N' => (str)strtotime("now")]             ]         ]); 

from documantaion:

Item  Each element in the Item map is an AttributeValue object.  **Type: String to AttributeValue object map** 


回答2:

$result = $DynamoDb->putItem([       'TableName' => 'products',         'Item' => [           'product_code' => ['N'  =>  (string)$row[0]],           'created_at' => ['N' => (string)time()]         ]     ]); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!