Configuring Doctrine DBAL

Nice features a Doctrine DBAL bridge that integrates the two, exposing a few useful services. Usage is simple with only the installation of a package and some configuration.

First, add the Nice Doctrine DBAL extension to your project. You can do this by updating your composer.json or running composer require at the command line.

  • Example composer.json:

    {
        "require": {
            "nice/framework": "~1.0",
            "nice/doctrine-dbal": "~1.0"
        }
    }

    Then run composer update at the command line.

  • Using the composer command line tool

    composer require nice/doctrine-dbal:~1.0

Once Doctrine DBAL is installed, open your front controller (usually web/index.php) and add the following:

use Nice\Extension\DoctrineDbalExtension;

// ...

$app = new Application();
$app->appendExtension(new DoctrineDbalExtension(array(
    'database' => array(
        'driver' => 'pdo_sqlite',
        'path' => '%app.root_dir%/sqlite.db'
    )
)));

Two new services will now be available:

  • doctrine.dbal.database_connection is the connection to the database. It is an instance of Doctrine\DBAL\Connection.
  • doctrine.dbal.configuration is the Configuration instance for the database.

Now, assuming you've created sqlite.db in your app's root directory, create the messages table:

CREATE TABLE messages
(
    id INTEGER PRIMARY KEY NOT NULL,
    message TEXT NOT NULL
);
INSERT INTO messages (1, "This is a message!");

Then, in your web/index.php where you define your routes, and add a new route:

use Symfony\Component\HttpFoundation\JsonResponse;

// ...
// Configure your routes
$app->set('routes', function (RouteCollector $r) {
    // ...

    $r->map('/messages', null, function (Application $app, Request $request) {
        $conn = $app->get('doctrine.dbal.database_connection');
        $results = $conn->executeQuery("SELECT * FROM messages")->fetchAll();

        return new JsonResponse($results);
    }); 
});

Visiting your site at index.php/messages should result in the following JSON output:

[{"id":"1","message":"This is a message!"}]

Powered by Codex 1.1.0