Book Image

DynamoDB Cookbook

By : Tanmay Deshpande
Book Image

DynamoDB Cookbook

By: Tanmay Deshpande

Overview of this book

Table of Contents (18 chapters)
DynamoDB Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Updating an item in the DynamoDB table using the AWS SDK for PHP


Let's understand how to update an item from the DynamoDB table using the AWS SDK for PHP.

Getting ready

To perform this operation, you can use the IDE of your choice.

How to do it…

Let's try to understand how to update a stored item in the DynamoDB table using PHP. The update request allows us to add a new attribute to the item, update the values of an attribute, or remove an attribute from the item:

  1. Create an instance of the DynamoDBClient and initialize it with the desired AWS region and credentials:

    $client = DynamoDbClient::factory(array(
        'profile' => 'default',
        'region' => 'us-east-1' 
    ));
  2. Create the Update item request and specify the details. Here, I want to add an attribute called rating with the value as 5, update/reduce the value of stock by 2, and remove the mnfr attribute from that item whose primary key id is 10 and the type phone:

    $response = $client->updateItem(array(
        'TableName' => 'productTable...