Connecting to Crate with PHP

Crate supports integration with the PDO and DBAL data abstraction layers.

PDO

Install Driver

Install the driver by adding a dependency to your composer.json file:

{
  "require": {
    "crate/crate-pdo":"~0.3.0"
  }
}

And install with composer intall.

Connect to Crate

To connect to your cluster, Crate follows standard PDO syntax to form a data source name string (dsn) and then connect to it.

require_once __DIR__.'/vendor/autoload.php';
use Crate\PDO\PDO as PDO;

$dsn = 'crate:SERVER_IP:4200';
$connection = new PDO($dsn, null, null, null);

As Crate doesn't support authentication, the other parameters can be left null.

DBAL

Install Driver

Install the driver by adding a dependency to your composer.json file:

{
  "require": {
    "crate/crate-dbal":"~0.2.0"
  }
}

And install with composer intall.

Connect to Crate

To connect to your cluster, Crate follows standard DBAL syntax to create an array of parameters and then create a connection.

$params = array(
    'driverClass' => 'Crate\DBAL\Driver\PDOCrate\Driver',
    'host' => 'SERVER_IP',
    'port' => 4200
);

$connection = \Doctrine\DBAL\DriverManager::getConnection($params);
$schemaManager = $connection->getSchemaManager();

ORM

If you are using Doctrine's ORM features, then an extra dependency is needed in your composer.json file.

{
  "require": {
    "crate/crate-dbal":"~0.2.0",
    "doctrine/orm": "*"
  }
}

And install with composer intall.

To create a connection:

require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array("/path/to/entity-files");
$isDevMode = false;

// the connection configuration
$params = array(
    'driverClass' => 'Crate\DBAL\Driver\PDOCrate\Driver',
    'host' => 'SERVER_IP',
    'port' => 4200
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($params, $config);

Setting up the Doctrine ORM requires some extra steps, and we suggest reading the official Doctrine documentation to get started.

Want to Know More?

  • More detailed documentation on the Crate PDO client is available here.
  • More detailed documentation on the Crate DBAL client is available here.
  • The Crate Sample App demonstrates features of Crate and how to use them in a PHP based application with an explanation of how the library is used here.

Next Steps