revenuewire / dynamodb-orm
RW Dynamo ORM
v2.0.7
2023-01-30 21:53 UTC
Requires
- aws/aws-sdk-php: ^3.257
This package is auto-updated.
Last update: 2024-09-20 00:14:34 UTC
README
composer require revenuewire/dynamodb-orm
配置
<?php if (APPLICATION_ENV == "local" || APPLICATION_ENV == "qa") { Model::configure(["region" => NETWORK_REGION, "endpoint" => 'http://dynamodb:8000']); } else { Model::configure(["region" => NETWORK_REGION]); }
模型
手动创建对象类。
<?php use RW\DynamoDb\Model; class User extends Model { public static $tableName = 'user'; /** * DynamoDB Schema Definition */ public static $schema = [ "TableName" => "user", "AttributeDefinitions" => [ [ 'AttributeName' => 'id', 'AttributeType' => 'S', ] ], 'KeySchema' => [ [ 'AttributeName' => 'id', 'KeyType' => 'HASH', ] ], 'ProvisionedThroughput' => [ 'ReadCapacityUnits' => 5, 'WriteCapacityUnits' => 5, ], ]; }
安装数据库
<?php require_once (__DIR__ . "/../vendor/autoload.php"); Model::configure(["region" => NETWORK_REGION]); $schemas = [ \Models\User::$schema, ]; echo "Install DBs..."; foreach ($schemas as $schema) { try { Model::$client->deleteTable([ "TableName" => $schema['TableName'] ]); } catch (Exception $e) {} Model::$client->createTable($schema); } echo "done\n";
使用方法
创建
<?php $user = new User(); $user->id = "my-id"; $user->firstName = "hello"; $user->lastName = "world"; $user->save();
查找并更新
<?php $user = User::getById('my-id'); $user->lastName = "wood"; $user->save();