Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Example: Batch Operations Using AWS SDK for PHP

Example: Batch Write Operation Using the AWS SDK for PHP

The following PHP code example does the following tasks:

  • Puts an item in the Forum table.

  • Puts and deletes an item in the Thread table.

To learn more about the batch write operation, see Batch Write: Putting and Deleting Multiple Items.

This code example assumes that you have followed the steps in Creating Tables and Loading Sample Data and have created the Forum and Thread tables. Alternatively, you can load the data programmatically using the instructions in the Creating Example Tables and Uploading Data Using the AWS SDK for PHP topic.

Note

For step-by-step instructions to test the following code example, see PHP Code Samples.

<?php
require 'vendor/autoload.php';

date_default_timezone_set('UTC');

use Aws\DynamoDb\Exception\DynamoDbException;

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

$dynamodb = $sdk->createDynamoDb();

$tableNameOne = 'Forum';
$tableNameTwo = 'Thread';

$response = $dynamodb->batchWriteItem([
    'RequestItems' => [
        $tableNameOne => [
            [
                'PutRequest' => [
                    'Item' => [
                        'Name'   => ['S' => 'Amazon S3 Forum'],             
                        'Threads' => ['N' => '0']
                    ]]
            ]
        ],          
         $tableNameTwo => [
            [
                'PutRequest' => [
                    'Item' => [
                        'ForumName'   => ['S' => 'Amazon S3 Forum'],             
                        'Subject' => ['S' => 'My sample question'],
                        'Message'=> ['S' => 'Message Text.'],
                        'KeywordTags'=>['SS' => ['Amazon S3', 'Bucket']]
                    ]]
            ],
            [
                'DeleteRequest' => [
                    'Key' => [
                        'ForumName' =>['S' => 'Some partition key value'],
                        'Subject' => ['S' => 'Some sort key value']
                    ]]
                ]
         ]
    ]
]);

print_r($response);

?>