1. Go to this page and download the library: Download oasis/dynamodb-odm 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/ */
oasis / dynamodb-odm example snippets
use Oasis\Mlib\ODM\Dynamodb\ItemManager;
$awsConfig = [
"profile" => "oasis-minhao",
"region" => "ap-northeast-1"
];
$tablePrefix = "odm-";
$itemNamespace = 'Oasis\Mlib\ODM\Dynamodb\Ut'; // in practice, this usually looks like: My\Root\Namespace\Items
$itemSrcDir = __DIR__ . "/ut"; // in practice, this usually points to src/Items directory
$im = new ItemManager(
$awsConfig,
$tablePrefix,
);
$im->addNamespace(
$itemNamespace,
$itemSrcDir
);
use Oasis\Mlib\ODM\Dynamodb\Console\ConsoleHelper;
// replace with file to your own project bootstrap
w ConsoleHelper($itemManager);
use Oasis\Mlib\ODM\Dynamodb\Attributes\Field;
use Oasis\Mlib\ODM\Dynamodb\Attributes\Index;
use Oasis\Mlib\ODM\Dynamodb\Attributes\Item;
#[Item(
table: 'users',
primaryIndex: new Index(hash: 'id'),
)]
class User
{
#[Field(type: 'number')]
protected int $id = 0;
#[Field(type: 'string')]
protected ?string $name = null;
}
#[Item(
table: 'users',
primaryIndex: new Index(hash: 'id'),
globalSecondaryIndices: [
new Index(hash: 'class', range: 'age', name: 'class-age-gsi'),
],
)]
class User
{
// ...
#[Field]
protected ?string $class = null;
#[Field(type: 'number')]
protected int $age = 0;
}
/** @var ItemManger $im */
/** @var User $user */
$im->detach($user);
$user->setName('Mr.Left');
$im->flush(); // changes to $user will not be synchronized
/** @var ItemManager $im */
/** @var ItemRepository $userRepo */
$userRepo = $im->getRepository(User::class);
/** @var Users[] $users */
$users = $userRepo->multiQueryAndRun(
function ($item) {
// each item returned can be accessed here in the callback
},
"classPartition", // PartitionedHashKey field name
"A", // value expected in the base field (not the partition field)
"#age >= :minAge", // only range conditions here
[
":minAge" => 25,
],
"class-partition-age-index" // index for PartitionedHashKey
);