php-dynamodb Documentation

php-dynamodb is a PHP library that can be used to interact with Amazon DynamoDB. It provides a layer of abstraction between your code and the DynamoDB-related classes made available by the AWS SDK for PHP.

Quickstart

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php declare(strict_types=1);

require dirname(__DIR__) . '/vendor/autoload.php';

use Guillermoandrae\DynamoDb\Constant\AttributeTypes;
use Guillermoandrae\DynamoDb\Constant\KeyTypes;
use Guillermoandrae\DynamoDb\DynamoDbAdapter;

// create a new adapter
$adapter = new DynamoDbAdapter();

try {
    $tableName = 'myTable';

    // create a table
    $adapter->useTable($tableName)->createTable([
        'year' => [AttributeTypes::NUMBER, KeyTypes::HASH],
        'title' => [AttributeTypes::STRING, KeyTypes::RANGE],
    ]);

    // add an item to the table
    $adapter->useTable($tableName)->insert([
        'year' => 2015,
        'title' => 'The Big New Movie',
        'info' => [
            'plot' => 'Nothing happens at all',
            'rating' => 0,
        ],
    ]);

    // fetch an item from the table
    $item = $adapter->useTable($tableName)->find([
        'year' => 2015,
        'title' => 'The Big New Movie'
    ]);

    printf('Added item: %s - %s' . PHP_EOL, $item['year'], $item['title']);

    print_r($item);

    // delete the table
    $adapter->useTable($tableName)->deleteTable();

} catch (\Exception $ex) {
    die($ex->getMessage() . PHP_EOL);
}