PHP code example of kettle / dynamodb-orm

1. Go to this page and download the library: Download kettle/dynamodb-orm 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/ */

    

kettle / dynamodb-orm example snippets



use Kettle\ORM;

$user = ORM::factory('User')->findOne(10);
$user->name = 'John';
$user->save();


$tweets = ORM::factory('Tweet')->where('user_id', 10)
                 ->findMany();

foreach ($tweets as $tweet) {
    echo $tweet->text . PHP_EOL;
}



use Kettle\ORM;

ORM::configure("key",    'AWS_KEY');
ORM::configure("secret", 'AWS_SECRET');
ORM::configure("region", 'AWS_REGION');

// In order to use DynamoDB Local, you need to set "base_url".
// ORM::configure("base_url", 'http://localhost:8000/');



use Kettle\ORM;

ORM::configure("key",    'AWS_KEY',    'account-2');
ORM::configure("secret", 'AWS_SECRET', 'account-2');
ORM::configure("region", 'AWS_REGION', 'account-2');

$user = ORM::factory('User', 'account-2');




class User extends ORM {
    protected $_table_name = 'user';
    protected $_hash_key   = 'user_id';
    protected $_schema = array(
      'user_id'    => 'N',  // user_id is number
      'name'       => 'S',  // name is string
      'age'        => 'N',
      'country'    => 'S',
     );
}





$user = ORM::factory('User')->create();
$user->user_id = 1;
$user->name    = 'John';
$user->age     = 20;
$user->save();




$user = ORM::factory('User')->findOne(1);
echo $user->name. PHP_EOL;

print_r($user->asArray());




$user = ORM::factory('User')->findOne(1);
$user->age = 21;
$user->save();




$user = ORM::factory('User')->findOne(1);
$user->delete();




$tweets = ORM::factory('Tweets')
        ->where('user_id', 1)
        ->where('timestamp', '>', 1397264554)
        ->findMany();

foreach ($tweets as $tweet) {
     echo $tweet->text . PHP_EOL;
}




$tweet = ORM::factory('Tweets')
        ->where('user_id', 1)
        ->where('timestamp', '>', 1397264554)
        ->findFirst();

echo $tweet->text . PHP_EOL;




$users = ORM::factory('User')
        ->where('country', 'Japan')
        ->where('age', '>=', 20)
        ->index('country-age-index')  // specify index name
        ->findMany();




$tweets = ORM::factory('Tweets')
          ->where('user_id', 1)
          ->filter('is_deleted', 0) // using filter
          ->findMany();


$ php bin/kettle-skeleton.php --table-name user --region ap-northeast-1 > User.php