The DoctrineMongoDBServiceProvider provides a default Doctrine2 MongoDB ODM Connection and an DocumentManager.
use Knp\Silex\ServiceProvider\DoctrineMongoDBServiceProvider;
$app->register(new DoctrineMongoDBServiceProvider(), array(
'doctrine.odm.mongodb.connection_options' => array(,
'database' => 'my_database_name',
'host' => 'localhost',
)
));
// if you want to autoload your documents, use the autoloader service:
$app['autoloader']->registerNamespace('Document', __DIR__);
'doctrine.odm.mongodb.connection_options' => array(
'database' => 'my_database_name',
'host' => 'localhost',
),
This parameter provides options for the doctrine.mongodb.connection service.
'doctrine.odm.mongodb.documents' => array(
array('type' => 'yml', 'path' => '/path/to/yml/files', 'namespace' => 'My\\Document'),
array('type' => 'annotation', 'path' => array(
'/path/to/Documents',
'/path/to/another/dir/for/the/same/namespace'
), 'namespace' => 'Document'),
array('type' => 'annotation', 'path' => '/path/to/another/dir/with/documents', 'namespace' => 'Acme\\Document'),
array('type' => 'xml', 'path' => '/path/to/xml/files', 'namespace' => 'Your\\Document')
)
This is an advanced configuration example which replaces the default one:
array('type' => 'annotation', 'path' => 'Document', 'namespace' => 'Document')
Default behavior will search annotated Documents in the Document directory.
$category = $app['doctrine.odm.mongodb.dm']
->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.odm.mongodb.dm']->getEventManager()->addEventSubscriber($timestampableListener);