Using CRATE with Symfony2

Posted on: 2015-01-30 | Categories: Databse PHP

CRATE with Symfony2 Today I want to introduce to you great elastic data store Crate.io that I was working with lately. CRATE is a new way of building scalable applications: “Crate takes care of synchronization, sharding, scaling, and replication even for mammoth data sets.” Also I’m going to show how easily we can integrate it with PHP / Symfony2 framework – if we can call setting new DBAL driver as integration.

Crate

What is Crate.IO?

There is already really interesting overview: What is Crate? on the CRATE website – this is must-read introduction.

For us CRATE has everything we need:

Lately we were looking for some new solution to store our Call Detail Records which grow very quickly. MySQL database engine was doing quite well so far but with the beginning of the year it was time for change.
Before migration on table with about 30 million records, MySQL quries were executed in about 5 to 15 seconds. Now with CRATE it takes less than 500 milliseconds.

Installation

On my Linux Mint 17 (Cinnamon) I’ve installed CRATE with following steps:

Complete guide how to install CRATE can be found here.

Crate Admin

CRATE comes with a nice web based admin panel where we can for example: check Cluster status, manage tables or run SQL queries. Admin interface can be accessed with any browser by providing the following url in your browser:

CRATE Admin

Notice: Using CRATE Tutorial tab in Admin UI we can import some tweets for testing – that’s pretty useful.

CRASH – CRAte SHell

CRASH is another useful CRATE feature that can be used for testing purpose and not only. Using CRASH we can for example: create new table, insert / update / delete records or run other queries. Please read the documentation for full list of supported commands.

To start CRASH type this in your shell:

and from now you can type SQL commands.

Notice: In my environment I’ve created crash alias to avoid using long command – path can be different in different systems:

Crate - crash

The project

CRATE can be used in any PHP project thanks to Crate-PDO driver. In this tutorial we are going to use CRATE with Symfony2 and Doctrine2 ORM, that’s why we need Crate-DBAL which is already using Crate-PDO.

Setup Symfony2 project

Lets create sample project Symfony2 project (we can call it sfCrate):

More information can be found on Installing and Configuring Symfony2 page.

Notice: For now CRATE DBAL supports only Doctrine2 DBAL in version 2.3.5. This should be fixed be the end of next week but for now the following line is needed in project composer.json. CRATE has now support for the latest Doctrine2 DBAL version.

Then we need to install crate-dbal with command:

To complete integration process we need to modify app/config/config.yml file:

That’s all. From now we can use CRATE DBAL connection in Symfony2 with Doctrine2.

Sample usage:

CRATE DBAL has also support for SchemaManager excluding features that are not supported by CRATE like JOINs (under heavy development) or foreign keys.

If we want to use CRATE as extra connection in our project (new or existing), configuration should looks like this:

To get CRATE connection we need to change to following line.

I hope this will help you start working with CRATE and Symfony2. I can assure you that it’s worth giving a go.