The DoctrineServiceProvider provides a default Doctrine2 DBAL Connection and an EntityManager.
use Knp\Silex\ServiceProvider\DoctrineServiceProvider;
$app->register(new DoctrineServiceProvider(), array(
'doctrine.dbal.connection_options' => array(,
'driver' => 'pdo_sqlite',
'path' => ':memory'
),
'doctrine.orm' => true
));
// if you want to autoload your entities, use the autoloader service:
$app['autoloader']->registerNamespace('Entity', __DIR__);
'doctrine.dbal.connection_options' => array(
'driver' => 'pdo_sqlite',
'path' => ':memory'
),
or:
'doctrine.dbal.connection_options' => array(
'driver' => 'pdo_mysql',
'dbname' => 'my_database_name',
'host' => 'localhost',
'user' => 'root',
'password' => null
),
This parameter activates the doctrine.dbal.connection service.
These above are simple examples. Full documentation is available.
'doctrine.orm.entities' => array(
array('type' => 'yml', 'path' => '/path/to/yml/files', 'namespace' => 'My\\Entity'),
array('type' => 'annotation', 'path' => array(
'/path/to/Entities',
'/path/to/another/dir/for/the/same/namespace'
), 'namespace' => 'Entity'),
array('type' => 'annotation', 'path' => '/path/to/another/dir/with/entities', 'namespace' => 'Acme\\Entity'),
array('type' => 'xml', 'path' => '/path/to/xml/files', 'namespace' => 'Your\\Entity')
)
This is an advanced configuration example which replaces the default one:
array('type' => 'annotation', 'path' => 'Entity', 'namespace' => 'Entity')
Default behavior will search annotated Entities in the Entity directory.
$categories = $app['doctrine.dbal.connection']->query('SELECT * FROM category')->fetchAll();
$category = $app['doctrine.orm.em']->getRepository('Acme\Entity\Category')->findOneBy(array('name' => 'Category A'));
This is an example of how to add a Timestampable behavior to Doctrine. ( http://gediminasm.org/article/timestampable-behavior-extension-for-doctrine-2 )
// if you need autoloading of external lib
$app['autoloader']->registerNamespace('Gedmo', __DIR__.'/vendor/Gedmo/DoctrineExtensions/lib');
$timestampableListener = new \Gedmo\Timestampable\TimestampableListener();
$app['doctrine.dbal.event_manager']->addEventSubscriber($timestampableListener);