This bundle provides a symfony 2 service to interact with cpliakas/dynamo-db-odm.
Installation is a quick 3 step process:
- Download Zeliard91DynamoDBConnectorBundle using composer
- Enable the Bundle
- Configure your application's config.yml
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.
Enable the bundle in the kernel:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Zeliard91\Bundle\DynamoDBConnectorBundle\Zeliard91DynamoDBConnectorBundle(),
);
}
# 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
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();
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();
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