Commit 79b0e1b4 authored by Greg Militello's avatar Greg Militello Committed by Fabien Potencier

updated DoctrineExtension to support multiple databases.

parent bda57d54
......@@ -11,7 +11,7 @@ The *DoctrineExtension* provides integration with the `Doctrine DBAL
Parameters
----------
* **db.options**: Array of Doctrine DBAL options.
* **dbal.options**: Array of Doctrine DBAL options.
These options are available:
......@@ -32,25 +32,29 @@ Parameters
* **path**: Only relevant for ``pdo_sqlite``, specifies the path to
the SQLite database.
* **default**: (optional) When using multiple databases, allows you
to set the default connection to one other than the first
connection
These and additional options are described in detail in the `Doctrine DBAL
configuration documentation <http://www.doctrine-project.org/docs/dbal/2.0/en/reference/configuration.html>`_.
* **db.dbal.class_path** (optional): Path to where the
* **dbal.dbal.class_path** (optional): Path to where the
Doctrine DBAL is located.
* **db.common.class_path** (optional): Path to where
* **dbal.common.class_path** (optional): Path to where
Doctrine Common is located.
Services
--------
* **db**: The database connection, instance of
* **dbal**: The database connection, instance of
``Doctrine\DBAL\Connection``.
* **db.config**: Configuration object for Doctrine. Defaults to
* **dbal.config**: Configuration object for Doctrine. Defaults to
an empty ``Doctrine\DBAL\Configuration``.
* **db.event_manager**: Event Manager for Doctrine.
* **dbal.event_manager**: Event Manager for Doctrine.
Registering
-----------
......@@ -59,12 +63,12 @@ Make sure you place a copy of *Doctrine DBAL* in ``vendor/doctrine-dbal``
and *Doctrine Common* in ``vendor/doctrine-common``::
$app->register(new Silex\Extension\DoctrineExtension(), array(
'db.options' => array(
'dbal.options' => array(
'driver' => 'pdo_sqlite',
'path' => __DIR__.'/app.db',
),
'db.dbal.class_path' => __DIR__.'/vendor/doctrine-dbal/lib',
'db.common.class_path' => __DIR__.'/vendor/doctrine-common/lib',
'dbal.dbal.class_path' => __DIR__.'/vendor/doctrine-dbal/lib',
'dbal.common.class_path' => __DIR__.'/vendor/doctrine-common/lib',
));
Usage
......@@ -75,11 +79,73 @@ example::
$app->get('/blog/show/{id}', function ($id) use ($app) {
$sql = "SELECT * FROM posts WHERE id = ?";
$post = $app['db']->fetchAssoc($sql, array((int) $id));
$post = $app['dbal']->fetchAssoc($sql, array((int) $id));
return "<h1>{$post['title']}</h1>".
"<p>{$post['body']}</p>";
});
Using multiple databases
------------------------
The Doctrine extension can allow access to multiple databases. In order
configure these data sources you must remove the **db.options** from
your extension registration, and replace it with an array named **dbs**.
Each key of the dbs array should contain a configuration of options.
Registering multiple database connections::
$app->register(new Silex\Extension\DoctrineExtension(), array(
'dbal.dbs' => array (
'sqlite' => array(
'driver' => 'pdo_sqlite',
'path' => __DIR__.'/app.db',
),
'mysql_read' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_read.someplace.tld'
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
),
'mysql_write' => array(
'driver' => 'pdo_mysql',
'host' => 'mysql_write.someplace.tld'
'dbname' => 'my_database',
'user' => 'my_username',
'password' => 'my_password',
),
),
'dbal.dbal.class_path' => __DIR__.'/vendor/doctrine-dbal/lib',
'dbal.common.class_path' => __DIR__.'/vendor/doctrine-common/lib',
));
By default, the first connection registered is the default. This can simply be accessed as you would if there was only one connection. Given the above DB registration these two lines are equal:
$app['dbal']->fetchAssoc('SELECT * FROM table');
$app['dbal.connection.sqlite']->fetchAssoc('SELECT * FROM table');
The default connection can be selected by setting the **default** option toggle.
Using multiple connections::
$app->get('/joined/{searchOne}/{searchTwo}, function ($searchOne, $searchTwo) use ($app)) {
$sqliteQuery = "SELECT * FROM table_one WHERE id = ?";
$one = $app['dbal.connection.sqlite']->fetchAssoc($sqliteQuery, array((int) $searchOne));
$mysqlQuery = "SELECT * FROM table_two WHERE id = ?";
$two = $app['dbal.connection.mysql_read']->fetchAssoc($mysqlQuery, array((int) $searchTwo));
$mysqlUpdate = "UPDATE table_two SET value = ? WHERE id = ?";
$app['dbal.connection.mysql_write']->execute($mysqlUpdate, array((int) $searchTwo, 'newValue'));
return "<h1>{$one['column_from_sqlite']}</h1>".
"<p>{$two['column_from_mysql']}</p>";
});
For more information, consult the `Doctrine DBAL documentation
<http://www.doctrine-project.org/docs/dbal/2.0/en/>`_.
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
class DoctrineDbalExtension implements ExtensionInterface
{
public function register(Application $app)
{
if (isset($app['dbal.options'])) {
$app['dbal.dbs'] = array('default' => $app['dbal.options']);
}
if (isset($app['dbal.dbs']) && is_array($app['dbal.dbs'])) {
$firstConnection = true;
foreach ($app['dbal.dbs'] as $connection => $options) {
$app['dbal.connection.'.$connection.'.options'] = $options;
$app['dbal.connection.'.$connection] = $app->share(function () use ($app, $options, $connection) {
return DriverManager::getConnection($options, $app['dbal.connection.'.$connection.'.config'], $app['dbal.connection.'.$connection.'.event_manager']);
});
$app['dbal.connection.'.$connection.'.config'] = $app->share(function () {
return new Configuration();
});
$app['dbal.connection.'.$connection.'.event_manager'] = $app->share(function () {
return new EventManager();
});
if ($firstConnection || !empty($options['default'])) {
$app['dbal'] = function() use ($app, $connection) {
return $app['dbal.connection.'.$connection];
};
}
$firstConnection = false;
}
unset($app['dbal.dbs']);
}
if (isset($app['dbal.dbal.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\DBAL', $app['dbal.dbal.class_path']);
}
if (isset($app['dbal.common.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\Common', $app['dbal.common.class_path']);
}
}
}
\ No newline at end of file
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
class DoctrineExtension implements ExtensionInterface
{
public function register(Application $app)
{
$app['db.options'] = array_replace(array(
'driver' => 'pdo_mysql',
'dbname' => null,
'host' => 'localhost',
'user' => 'root',
'password' => null,
), isset($app['db.options']) ? $app['db.options'] : array());
$app['db'] = $app->share(function () use($app) {
return DriverManager::getConnection($app['db.options'], $app['db.config'], $app['db.event_manager']);
});
$app['db.config'] = $app->share(function () {
return new Configuration();
});
$app['db.event_manager'] = $app->share(function () {
return new EventManager();
});
if (isset($app['db.dbal.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\DBAL', $app['db.dbal.class_path']);
}
if (isset($app['db.common.class_path'])) {
$app['autoloader']->registerNamespace('Doctrine\\Common', $app['db.common.class_path']);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment