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

Working with Tables: PHP

You can use the AWS SDK for PHP to create, update, and delete tables, list all the tables in your account, or get information about a specific table.

The following are the common steps for DynamoDB operations using the SDK for PHP:

  1. Create an instance of the DynamoDB client.

  2. Provide the parameters for a DynamoDB operation, including any optional parameters.

  3. Load the response from DynamoDB into a local variable for your application.

Creating a Table

To create a table, you must provide the table name, its primary key, and the provisioned throughput values. The following PHP code sample creates an ExampleTable that uses a numeric type attribute Id as its primary key.

The following PHP code snippet creates a table (ProductCatalog) that uses Id as the primary key and set of provisioned throughput values. Depending on your application requirements, you can update the provisioned throughput values by using the updateTable method.


require 'vendor/autoload.php';

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

$dynamodb = $sdk->createDynamoDb();

$tableName = 'ExampleTable';

echo "# Creating table $tableName...\n";

$result = $dynamodb->createTable([
    'TableName' => $tableName,
    'AttributeDefinitions' => [
        [ 'AttributeName' => 'Id', 'AttributeType' => 'N' ]
    ],
    'KeySchema' => [
        [ 'AttributeName' => 'Id', 'KeyType' => 'HASH' ]
    ],
    'ProvisionedThroughput' => [
        'ReadCapacityUnits'    => 5, 
        'WriteCapacityUnits' => 6
    ]
]);

print_r($result->getPath('TableDescription'));

You must wait until DynamoDB creates the table and sets the table status to ACTIVE before you can put data into the table. You can use the client's waitUntil function to wait until the table's status becomes ACTIVE.

The following code snippet demonstrates how to do this. The associative array @waiter is optional, letting you specify the polling options for waitUntil.


$dynamodb->waitUntil('TableExists', [
    'TableName' => $tableName,
    '@waiter' => [
        'delay'       => 5,
        'maxAttempts' => 20
    ]
]);

Updating a Table

You can update an existing table, modifying its provisioned throughput values or its secondary indexes.

Immediately after a successful UpdateTable request, the table status will be UPDATING. When the operation has completed, the table returns to the ACTIVE state.

The following PHP code snippet modifies a table's provisioned throughput settings. It then waits until the table becomes ACTIVE again.


$tableName = 'ExampleTable';

echo "Updating provisioned throughput settings on $tableName...\n";

$result = $dynamodb->updateTable([
    'TableName' => $tableName,
    'ProvisionedThroughput'    => [
        'ReadCapacityUnits'    => 6,        
        'WriteCapacityUnits' => 7          
    ]
]);


$dynamodb->waitUntil('TableExists', [
    'TableName' => $tableName,
    '@waiter' => [
        'delay'       => 5,
        'maxAttempts' => 20
    ]
]);

echo "New provisioned throughput settings:\n";

echo "Read capacity units: " 
    .  $result['TableDescription']['ProvisionedThroughput']['ReadCapacityUnits'] 
    . "\n";
echo "Write capacity units: " 
    . $result['TableDescription']['ProvisionedThroughput']['WriteCapacityUnits'] 
    . "\n";

Deleting a Table

You can delete a table if you no longer need it.

Immediately after a successful DeleteTable request, the table status will be DELETING. When the operation has completed, the table will no longer exist.

The following PHP code snippet deletes a table, and waits for the operation to complete.


$tableName = 'ExampleTable';

echo "Deleting the table...\n";

$result = $dynamodb->deleteTable([
    'TableName' => $tableName
]);

$dynamodb->waitUntil('TableNotExists', [
    'TableName' => $tableName,
    '@waiter' => [
        'delay'       => 5,
        'maxAttempts' => 20
    ]
]);

echo "The table has been deleted.\n";

Listing Tables

You can use the ListTables operation to return the names of all of your tables in the current AWS region.

You can specify optional parameters. For example, you can set the Limit parameter if you want to use paging to limit the number of table names per page. You can also set the ExclusiveStartTableName parameter. After fetching the first page of results, DynamoDB returns a LastEvalutedTableName value. Use the LastEvalutedTableName value for the ExclusiveStartTableName parameter to get the next page of results.

The following PHP code snippet lists all of the tables that you own in the current AWS region. It uses the LastEvalutedTableName value for the ExclusiveStartTableName parameter, with a Limit value of 2 table names per page.


$tables = [];
unset($response);

do {
    if (isset($response)) {
        $params = [
            'Limit' => 2,
            'ExclusiveStartTableName' => $response['LastEvaluatedTableName']
        ];
    }else {
        $params = ['Limit' => 2];
    }

    $response = $dynamodb->listTables($params);

    foreach ($response['TableNames'] as $key => $value) {
        echo "$value\n";
    }

    $tables = array_merge($tables, $response['TableNames']);

} while ($response['LastEvaluatedTableName']);

// Print total number of tables

echo "Total number of tables: ";
print_r(count($tables));
echo "\n";