sj.meyer
sj.meyer

Reputation: 885

DynamoDB PHP update-item

I have a simple small table which I just want to update. Spent hours in the docs and testing things out, can't figure it out.

My table:

|environment |who|
-------------------
|ax          |tom|

Primary key is on 'environment'

All I want to do is to set 'who' to 'ben' where 'environment' = 'ax'.

I have tried both the legacy way of doing things, and the expression way. I also tried both the square brackets notation AWS uses in their docs, and the array() notation of php. Below are some of the ways I've tried doing it.

1:

$result = $client->updateItem(array(
    'ConditionExpression' => 'environment = :env',
    'ExpressionAttributeValues' => array(
        ':env' => array(
            'S' => 'environment'
        ),
        ':who' => array(
            'S' => $who
        )
    ),
    'Key' => array( // REQUIRED
        ':env' => array(
            'S' => $env
        )
    ),
    'ReturnValues' => 'UPDATED_NEW',
    'TableName' => 'areas',
    'UpdateExpression' => 'SET who = :who'
));

2:

$result = $client->updateItem([
    'ConditionExpression' => 'environment = :env',
    'ExpressionAttributeValues' => [
        ':env' => [
            'S' => 'environment'
        ],
        ':who' => [
            'S' => $who
        ]
    ],
    'Key' => [
        ':env' => [
            'S' => $env
        ]
    ],
    'ReturnValues' => 'UPDATED_NEW',
    'TableName' => 'areas',
    'UpdateExpression' => 'SET who = :who'
]);

3:

$result = $client->updateItem(array(
    'AttributeUpdates' => array(
        'who' => array(
            'Action' => 'PUT',
            'Value' => array(
                'S' => $who
            )
        )
    ),
    'Key' => array(
        'environment' => array(
            'S' => $env
        )
    ),
    'TableName' => 'areas'
));

Edit: This is the error I'm getting:

 SerializationException (client): Start of list found where not expected - {"__type":"com.amazon.coral.service#SerializationException","Message":"Start of list found where not expected"}'

Any help with this is much appreciated.

Upvotes: 1

Views: 3256

Answers (1)

Deepak Daniel
Deepak Daniel

Reputation: 102

I had encountered a similar problem. This is the code that i used and it worked.

$RegID = "abracadabra";
$tableName="DefaultDelivery";
$marshaler = new Marshaler();

$requested_delivery = '{"Packet0":{"PacketNo":"2","Quantity":"1000ml","Type":"Toned Milk"},"Packet2":{"PacketNo":"4","Quantity":"250ml","Type":"Toned Milk"}}';

$eav = $marshaler->marshalJson('
{
 ":RequestedDelivery" : '.$requested_delivery.'
}
');

$key = $marshaler->marshalJson('                                               
     {                                                                          
         "RegistrationID" : "'.$RegID.'"
     }                                                                          
');

$params = [
     'TableName' => "$tableName",
     'Key' => $key,
     'ExpressionAttributeValues' => $eav,
     'UpdateExpression' => 'SET RequestedDelivery = :RequestedDelivery',
     'ReturnValues' => 'UPDATED_NEW'
];

try {
    $result = $client->updateItem($params);
        echo "SUCCESS";
}
catch (DynamoDbException $e){
   echo "Unable to update Item : \n";
}

Upvotes: 2

Related Questions