PHP code example of pluginscart / dynamodb-php

1. Go to this page and download the library: Download pluginscart/dynamodb-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pluginscart / dynamodb-php example snippets




e_once '/path/to/DynamoDBWrapper.php'; // this module

$ddb = new DynamoDBWrapper(array(
    'key'    => 'YOUR_KEY',
    'secret' => 'YOUR_SECRET_KEY',
    'region' => 'SOME_REGION',
    'version' => 'latest'
));


$key = array(
    'UserId' => 2,
);
$result = $ddb->get('User', $key);
/*
Array
(
    [Name] => User2
    [UserId] => 2
)
*/



$keys = array(
    array(
        'UserId' => 2,
    ),
    array(
        'UserId' => 3,
    ),
);
$result = $ddb->batchGet('User', $keys);
/*
Array
(   
    [0] => Array
        (   
            [Name] => User 2
            [UserId] => 2
        )
    [1] => Array
        (   
            [Name] => User 3
            [UserId] => 3
        )
)
*/



$keyConditions = array(
    'UserId' => 2,
);
$result = $ddb->query('Friend', $keyConditions);
/*
Array
(
    [0] => Array
        (
            [UserId] => 2
            [FriendUserId] => 3
        )
    [1] => Array
        (
            [UserId] => 2
            [FriendUserId] => 4
        )
    ...
)
*/



$filter = array(
    'UserId'       => array('GT', 1),
    'FriendUserId' => array('IN', array(4, 5)),
);
$result = $ddb->scan('Friend', $filter);
/*
Array
(
    [0] => Array
        (
            [UserId] => 2
            [FriendUserId] => 4
        )
    [1] => Array
        (
            [UserId] => 2
            [FriendUserId] => 5
        )
    ...
)
*/



$keyConditions = array(
    'UserId' => 2,
);
$result = $ddb->count('Friend', $keyConditions);
// 5



$item = array(
    'UserId' => 1,
    'Name'   => 'User N',
);
$result = $ddb->put('User', $item);
// true, if success



$item = array(
    'UserId' => 1,
    'Name'   => 'User 1',
);
$expected = array(
    'UserId' => array('Exists' => false)
);
$result = $ddb->put('User', $item, $expected);
// false, if UserId = 1 exists



$items = array(
    array(
        'UserId' => 2,
        'Name'   => 'User2',
    ),
    array(
        'UserId' => 3,
        'Name'   => 'User3',
    ),
);
$result = $ddb->batchPut('User', $items);
// true, if success



$key = array(
    'UserId' => 2,
);
$update = array(
    'Name' => array('PUT', 'New User Name')
);
$result = $ddb->update('User', $key, $update);
/*
Array
(
    [Name] => New User Name
)
*/



$key = array(
    'UserId'       => 2,
    'FriendUserId' => 4,
);
$result = $ddb->delete('Friend', $key);
/*
Array
(
    [UserId] => 2
    [FriendUserId] => 4
)
*/



$keys = array(
    array(
        'UserId'       => 2,
        'FriendUserId' => 3,
    ),
    array(
        'UserId'       => 2,
        'FriendUserId' => 4,
    ),
    ...
);
$result = $ddb->batchDelete('Friend', $keys);
// true, if success



$result = $ddb->createTable('User', 'UserId');
// Create a table 'User', which has a hash key 'UserId' as STRING.
 
$result = $ddb->createTable('User', 'UserId::N');
// Create a table 'User', which has a hash key 'UserId' as NUMBER.
 
$result = $ddb->createTable('Friend', 'UserId', 'FriendUserId');
// Create a table 'Friend', which has a hash key 'UserId' as STRING and a range key 'FriendUserId' as STRING.



$options = array(
    'LocalSecondaryIndexes' => array(
        array('name' => "UserName",
              'type' => 'S',
              'projection_type' => 'KEYS_ONLY'
        ),
        array('name' => "CreatedAt",
              'type' => 'S',
              'projection_type' => 'KEYS_ONLY'
        )
    )
);
$result = $ddb->createTable('Friend', 'UserId', 'FriendUserId', $options);
// Create a table 'Friend', which has a hash key 'UserId' as STRING and a range key 'FriendUserId' as STRING and Local Secondary Index 'UserName' and 'CreatedAt' as both STRING.