zeliard91/dynamodb-connector-bundle

Symfony 2 DynamoDB connexion service designed to work with cpliakas/dynamo-db-odm

dev-master 2014-09-06 19:32 UTC

This package is auto-updated.

Last update: 2024-04-19 01:27:08 UTC


README

This bundle provides a symfony 2 service to interact with cpliakas/dynamo-db-odm.

Installation

Installation is a quick 3 step process:

  1. Download Zeliard91DynamoDBConnectorBundle using composer
  2. Enable the Bundle
  3. Configure your application's config.yml

Step 1: Download Zeliard91DynamoDBConnectorBundle using composer

Add Zeliard91DynamoDBConnectorBundle in your composer.json:

{
    "require": {
        "zeliard91/dynamodb-connector-bundle": "dev-master"
    }
}

Now tell composer to download the bundle by running the command:

$ php composer.phar update zeliard91/dynamodb-connector-bundle

Composer will install the bundle to your project's vendor/zeliard91/dynamodb-connector-bundle directory.

Step 2: Enable the bundle

Enable the bundle in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Zeliard91\Bundle\DynamoDBConnectorBundle\Zeliard91DynamoDBConnectorBundle(),
    );
}

Step3: Add your DynamoDB credentials in your project configuration file

# app/config/config.yml

zeliard91_dynamo_db_connector:
    # get the values from parameters.yml
    key: "%dynamodb_key%"
    secret: "%dynamodb_secret%"
    region: "eu-west-1"
    # optional : for dev, you can specify base url
    base_url: "%dynamodb_url%"
    # optional : location of your entities
    entity_namespaces: 
        - Foo\BarBundle\Entity

Usage

Access to service objects

You can get DynamoDB client and document manager in your application by calling the service.

<?php

$document_manager = $this->get('zeliard91_dynamo_db_connector')->getManager();
$dynamo_client    = $this->get('zeliard91_dynamo_db_connector')->getDynamoDb();
$schema_manager   = $this->get('zeliard91_dynamo_db_connector')->getSchemaManager();

Entity repositories

If you have register entity namespace, you can also create repositories classes in order to define queries.

Let's assume you have created the entity Foo\BarBundle\Entity\Book.php

Now define the repository class, it has to be in the same directory and must end by 'Repository' :

<?php
// Foo/BarBundle/Entity/BookRepository.php

namespace Foo\BarBundle\Entity;

use Zeliard91\Bundle\DynamoDBConnectorBundle\Repository\DefaultRepository as Repository;
use Cpliakas\DynamoDb\ODM\Conditions;
use Aws\DynamoDb\Enum\ComparisonOperator;

class BookRepository extends Repository
{
    /**
     * Find all books from an author
     * @param string $author
     * @return array
     */
    public function findByAuthor($author)
    {
        $conditions = Conditions::factory()
            ->addCondition('author', $author, ComparisonOperator::EQ)
        ;
        return $this->scan($conditions);
    }
}

You can now call the method in your controller :

<?php

$book_repository = $this->get('zeliard91_dynamo_db_connector')->getRepository('Book');
$books = $book_repository->findByAuthor($author);


// Here are some methods from the extended DefaultRepository

$book  = $book_repository->find($id);
$book  = $book_repository->find($id, $range); // if you have defined a range attribute
$books = $book_repository->findAll();

Schema Manager

By using this object, you can create, check or delete the table linked to your entity

<?php

$schema_manager = $this->get('zeliard91_dynamo_db_connector')->getSchemaManager();


$schema_manager->isTableExists('Book'); // returns true or false
$schema_manager->createTable('Book'); // throws exception if table already exists
$schema_manager->deleteTable('Book'); // throws exception if table does not exist