PHPackages                             cpliakas/dynamo-db-odm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. cpliakas/dynamo-db-odm

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

cpliakas/dynamo-db-odm
======================

A lightweight ODM for DynamoDB

0.3.5(11y ago)1116.2k2[5 issues](https://github.com/cpliakas/dynamo-db-odm/issues)MITPHPPHP &gt;=5.3.0

Since Apr 12Pushed 10y ago3 watchersCompare

[ Source](https://github.com/cpliakas/dynamo-db-odm)[ Packagist](https://packagist.org/packages/cpliakas/dynamo-db-odm)[ Docs](https://github.com/cpliakas/dynamo-db-odm)[ RSS](/packages/cpliakas-dynamo-db-odm/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (9)Versions (10)Used By (0)

DynamoDB ODM
============

[](#dynamodb-odm)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b638aa60654cc4546e209b940f5a26f960f209e0a33f84fab6a218699f11d3f5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63706c69616b61732f64796e616d6f2d64622d6f646d2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/cpliakas/dynamo-db-odm/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/40877985f436d8fe826f24cdb13974c5ddfac982d37fbe549720de69b5ee9e4f/68747470733a2f2f706f7365722e707567782e6f72672f63706c69616b61732f64796e616d6f2d64622d6f646d2f762f737461626c652e706e67)](https://packagist.org/packages/cpliakas/dynamo-db-odm)[![License](https://camo.githubusercontent.com/2b9ea3b0b0cf4e8e32e0af1e6976fe14797c64c789d60342aa7b3ac64d32fc10/68747470733a2f2f706f7365722e707567782e6f72672f63706c69616b61732f64796e616d6f2d64622d6f646d2f6c6963656e73652e737667)](https://packagist.org/packages/cpliakas/dynamo-db-odm)

A lightweight, no-frills ODM (Object Document Mapper) for [DynamoDB](http://aws.amazon.com/dynamodb/).

**NOTE:** This library only works with the 2.x version of the AWS SDK for PHP. The changes in the 3.x version of the SDK make it much easier to work with, and this library provides less value than before (which is a good thing, less code!). It is unclear as to whether or not a port to the 3.x SDK will be useful.

### Why?

[](#why)

Amazon provides an SDK that connects to DynamoDB. Why would you want to use an ODM on top of it?

- Allows developers to define their data model in the codebase
- Facilitates readable code by wrapping complex data structures with an OO API
- Adds logical extension points via [Symfony's EventDispatcher component](http://symfony.com/doc/current/components/event_dispatcher/introduction.html)
- Optionally enforces [entity integrity](http://en.wikipedia.org/wiki/Entity_integrity)
- Facilitates password hashing, data encryption, random string generation, etc.

Installation
------------

[](#installation)

DynamoDB ODM can be installed with [Composer](http://getcomposer.org)by adding it as a dependency to your project's composer.json file.

```
{
    "require": {
        "cpliakas/dynamo-db-odm": "*"
    }
}
```

Please refer to [Composer's documentation](https://github.com/composer/composer/blob/master/doc/00-intro.md#introduction)for more detailed installation and usage instructions.

Usage
-----

[](#usage)

### Defining Entities

[](#defining-entities)

Entities are classes that extend `Cpliakas\DynamoDb\ODM\Entity` and model different types of documents. Metadata, such as the table name and hash / range key attributes, are defined in static properties and accessed through the static methods defined in `Cpliakas\DynamoDb\ODM\EntityInterface`.

```
namespace Acme\Entity;

use Aws\DynamoDb\Enum\Type;
use Cpliakas\DynamoDb\ODM\Entity;

class Book extends Entity
{
    // The DynanoDB table name
    protected static $table = 'books';

    // The attribute containing the hash key
    protected static $hashKeyAttribute = 'isbn';

    // Optionally set the $rangeKeyAttribute static if appropriate

    // Optionally enforce entity integrity
    protected static $enforceEntityIntegrity = true;

    // Optionally map attributes to data types
    protected static $dataTypeMappings = array(
        'isbn' => Type::STRING,
    );

    // Optionally add attribute setters and getters to taste
    public function setIsbn($isbn)
    {
        $this->setAttribute('isbn', $isbn);
        return $this;
    }

    public function getIsbn()
    {
        return $this->getAttribute('isbn');
    }
}
```

*NOTE:* Other ODMs use [annotations](https://github.com/doctrine/annotations)to define metadata. This pattern can improve DX for applications with a large number of entities and improve performance when proper caching is implemented. However, this library intentionally chooses to use statics to define metadata since it is a lighter-weight solution for the applications this project is intended to be used in.

### Initializing The Document Manager

[](#initializing-the-document-manager)

The document manager is responsible for instantiating entity classes and reading / writing documents to DynamoDB.

```
require 'vendor/autoload.php';

use Aws\DynamoDb\DynamoDbClient;
use Cpliakas\DynamoDb\ODM\DocumentManager;

$dynamoDb = DynamoDbClient::factory(array(
    'key'    => '',
    'secret' => '',
    'region' => '',
));

$dm = new DocumentManager($dynamoDb);

// Register one or more namespaces that contain entities in order to avoid
// having to pass the fully qualified class names as arguments.
$dm->registerEntityNamesapce('Acme\Entity');
```

### CRUD Operations

[](#crud-operations)

Create a document.

```
// Instantiate the entity object to model the new document. "Book" is the
// entity's class name as defined in the "Defining Entities" example above.
$book = $dm->entityFactory('Book')
    ->setHashKey('0-1234-5678-9')
    ->setAttribute('title', 'The Book Title')
    ->setAttribute('author', 'Chris Pliakas')
;

// Documents can also act like arrays
$book['copyright'] = 2014;

// Save the document
$dm->create($book);

// Bulk insert
foreach($books as $book) {
    $dm->createBatch($book);
}
$dm->flush();
```

Read, update, and delete the document.

```
// Read the document
$book = $dm->read('Book', '0-1234-5678-9');

// Update the document
$book['title'] = 'Revised title';
$dm->update($book);

// Delete the document
$dm->delete($book);

// Bulk delete
foreach($books as $book) {
    $dm->deleteBatch($book);
}
$dm->flush();
```

*NOTE:* Other ODMs use the [unit of work pattern](http://robrich.org/archive/2012/04/18/design-patterns-for-data-persistence-unit-of-work-pattern-and.aspx)when persisting data to the backend. Due to the nature of DynamoDB and the desire to keep this library lightweight, we opted not to use this pattern.

### Composite Primary Keys

[](#composite-primary-keys)

Pass an array as the primary key parameter when an entity's table uses a hash and range primary key type.

```
// Assume that the "Thread" entity's table uses the hash and range primary key
// type containing the forumName and subject attributes.

// Load the document by the hash and range keys
$book = $dm->read('Thread', array('PHP Libraries', 'Using the DynamoDB ODM'));
```

### Query and Scan Commands

[](#query-and-scan-commands)

You can either pass the raw data structure defined by the AWS SDK for PHP as the second parameter or use the object oriented wrapper to build the search conditions. The example below uses the OO wrapper.

```
use Aws\DynamoDb\Enum\ComparisonOperator;

// Search for books published after 2010 that don't have the title "Do not read me"
$conditions = Conditions::factory()
    ->addCondition('title', 'Do not read me', ComparisonOperator::NE)
    ->addCondition('copyright', 2010, ComparisonOperator::GT)
;

// Search for books with existing attribute 'extra'
$conditions = Conditions::factory()
    ->addNotNullCondition('extra')
;

$result = $dm->scan('Book', $conditions);
```

### Attribute Transformers

[](#attribute-transformers)

Transformers convert attribute values set through the entity object to something else.

The following example builds upon the book entity above to transform `\DateTime`objects set as the `created` attribute to Unix timestamps.

```
namespace Acme\Entity;

use Cpliakas\DynamoDb\ODM\Entity;
use Cpliakas\DynamoDb\ODM\Renderer as Renderer;
use Cpliakas\DynamoDb\ODM\Transformer as Transformer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class Book extends Entity
{
    // Set statics here ...

    public function __construct(EventDispatcherInterface $dispatcher, $data = array())
    {
        parent::__construct($dispatcher, $data);
        $this->addTransformer('created', new Transformer\Date());
    }
}
```

Set a `\DateTime` object as the `created` attribute and create the document.

```
$time = new \DateTime();

$book = $dm->entityFactory('Book')
    ->setHashKey('0-1234-5678-9')
    ->setAttribute('created', $time)
;

$dm->create($book);
```

The value is stored as a Unix timestamp in DynamoDB.

### Attribute Renderers

[](#attribute-renderers)

Renderers convert the value stored in DynamoDB to something that is normalized or native to PHP when it is accessed.

The following example is the opposite of the use case above. It converts the Unix timestamp stored in DynamoDB to a `\DateTime` object.

Add the following statement to the `Book` object's constructor like we did with the transformer.

```
$this->addRenderer('created', new Renderer\Date());
```

Read the document from DynamoDB. Accessing the `created` attribute will return a `\DateTime` object.

```
$book = $dm->read('Book', '0-1234-5678-9');

echo $book['created']->format(\DateTime::ATOM);
```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance11

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~18 days

Recently: every ~30 days

Total

8

Last Release

4287d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/037ad5b988519fe019d4d20758d45d70ba67d7b3aa7badca6a0f4bfedb882754?d=identicon)[cpliakas](/maintainers/cpliakas)

---

Top Contributors

[![cpliakas](https://avatars.githubusercontent.com/u/482722?v=4)](https://github.com/cpliakas "cpliakas (6 commits)")[![zeliard91](https://avatars.githubusercontent.com/u/3148652?v=4)](https://github.com/zeliard91 "zeliard91 (3 commits)")[![luzeduardo](https://avatars.githubusercontent.com/u/770092?v=4)](https://github.com/luzeduardo "luzeduardo (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cpliakas-dynamo-db-odm/health.svg)

```
[![Health](https://phpackages.com/badges/cpliakas-dynamo-db-odm/health.svg)](https://phpackages.com/packages/cpliakas-dynamo-db-odm)
```

###  Alternatives

[baopham/dynamodb

Eloquent syntax for DynamoDB

4975.7M6](/packages/baopham-dynamodb)[doctrine/doctrine-module

Laminas Module that provides Doctrine basic functionality required for ORM and ODM modules

3957.9M116](/packages/doctrine-doctrine-module)[kitar/laravel-dynamodb

A DynamoDB based Eloquent model and Query builder for Laravel.

193675.3k1](/packages/kitar-laravel-dynamodb)[doctrine/doctrine-mongo-odm-module

Laminas Module which provides Doctrine MongoDB ODM functionality

86676.6k35](/packages/doctrine-doctrine-mongo-odm-module)[thecodingmachine/tdbm

The Database Machine is a PHP ORM that requires no configuration. The object model is deduced from the database model.

123174.9k6](/packages/thecodingmachine-tdbm)[cache/doctrine-adapter

A PSR-6 cache implementation using Doctrine. This implementation supports tags

151.0M21](/packages/cache-doctrine-adapter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
