diff --git a/docs/config.rst b/docs/config.rst
index 0a81c71a..f3cec7f7 100644
--- a/docs/config.rst
+++ b/docs/config.rst
@@ -18,11 +18,11 @@ Sample Configuration
document_managers:
default:
mappings:
- AcmeDemoBundle: ~
- filters:
- filter-name:
- class: Class\Example\Filter\ODM\ExampleFilter
- enabled: true
+ App:
+ is_bundle: false
+ dir: '%kernel.project_dir%/src/Document'
+ prefix: 'App\Document'
+ alias: App
metadata_cache_driver: array # array, service, apcu, memcached, redis
.. code-block:: xml
@@ -41,13 +41,35 @@ Sample Configuration
-
-
+
+ .. code-block:: php
+
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->connection('default')
+ ->server(env('MONGODB_URL')->default('mongodb://localhost:27017')->resolve())
+ ->options([]);
+
+ $config->defaultDatabase('hello_' . param('kernel.environment'));
+ $config->documentManager('default')
+ ->mapping('App')
+ ->isBundle(false)
+ ->dir(param('kernel.project_dir') . '/src/Document')
+ ->prefix('App\\Document')
+ ->alias('App')
+ ->metadataCacheDriver('array'); // array, service, apcu, memcached, redis
+ };
+}
+
+
.. tip::
If each environment requires a different MongoDB connection URI, you can
@@ -66,7 +88,7 @@ Sample Configuration
default:
server: '%env(resolve:MONGODB_URL)%'
-If you wish to use memcache to cache your metadata, you need to configure the
+If you wish to use memcached to cache your metadata, you need to configure the
``Memcached`` instance; for example, you can do the following:
.. configuration-block::
@@ -83,7 +105,7 @@ If you wish to use memcache to cache your metadata, you need to configure the
document_managers:
default:
mappings:
- AcmeDemoBundle: ~
+ App: ~
metadata_cache_driver:
type: memcached
class: Symfony\Component\Cache\Adapter\MemcachedAdapter
@@ -103,7 +125,7 @@ If you wish to use memcache to cache your metadata, you need to configure the
-
+
Symfony\Component\Cache\Adapter\MemcachedAdapter
localhost
@@ -118,6 +140,28 @@ If you wish to use memcache to cache your metadata, you need to configure the
+ .. code-block:: php
+
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
+ use Symfony\Component\Cache\Adapter\MemcachedAdapter;
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->defaultDatabase('hello_' . param('kernel.environment'));
+ $config->connection('default')
+ ->server('mongodb://localhost:27017')
+ ->options([]);
+
+ $config->documentManager('default')
+ ->mapping('App')
+ ->metadataCacheDriver()
+ ->type('memcached')
+ ->class(MemcachedAdapter::class)
+ ->host('localhost')
+ ->port(11211)
+ ->instanceClass(\Memcached::class)
+ ;
+ };
Mapping Configuration
---------------------
@@ -138,9 +182,9 @@ can control. The following configuration options exist for a mapping:
- ``prefix`` A common namespace prefix that all documents of this mapping
share. This prefix should never conflict with prefixes of other defined
mappings otherwise some of your documents cannot be found by Doctrine. This
- option defaults to the bundle namespace + ``Document``, for example for an
- application bundle called ``AcmeHelloBundle``, the prefix would be
- ``Acme\HelloBundle\Document``.
+ option defaults to the application namespace + ``Document``, for example
+ for an application called ``App``, the prefix would be
+ ``App\Document``.
- ``alias`` Doctrine offers a way to alias document namespaces to simpler,
shorter names to be used in queries or for Repository access.
@@ -175,14 +219,14 @@ The following configuration shows a bunch of mapping examples:
document_managers:
default:
mappings:
- MyBundle1: ~
- MyBundle2: xml
- MyBundle3: { type: attribute, dir: Documents/ }
- MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping }
- MyBundle5:
+ App: ~
+ App2: xml
+ App3: { type: attribute, dir: Documents/ }
+ App4: { type: xml, dir: config/doctrine/mapping }
+ App5:
type: xml
- dir: my-bundle-mappings-dir
- alias: BundleAlias
+ dir: my-app-mappings-dir
+ alias: AppAlias
doctrine_extensions:
type: xml
dir: "%kernel.project_dir%/src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents"
@@ -201,10 +245,10 @@ The following configuration shows a bunch of mapping examples:
-
-
-
-
+
+
+
+
+ .. code-block:: php
+
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->documentManager('default')
+ ->mapping('App')
+ ->mapping('App2')
+ ->type('xml')
+ ->mapping('App3')
+ ->type('attribute')
+ ->dir('Documents/')
+ ->mapping('App4')
+ ->type('xml')
+ ->dir('config/doctrine/mapping')
+ ->mapping('App5')
+ ->type('xml')
+ ->dir('my-app-mappings-dir')
+ ->alias('AppAlias')
+ ->mapping('doctrine_extensions')
+ ->type('xml')
+ ->dir(param('kernel.project_dir') . '/src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents')
+ ->prefix('DoctrineExtensions\\Documents\\')
+ ->alias('DExt')
+ ;
+ }
+
+
Custom Types
------------
@@ -244,6 +317,16 @@ your documents.
+ .. code-block:: php
+
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->type('custom_type')
+ ->class(\Fully\Qualified\Class\Name::class)
+ ;
+ }
+
Filters
-------
@@ -298,6 +381,24 @@ Filters may be registered with a document manager by using the following syntax:
+ .. code-block:: php
+
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->documentManager('default')
+ ->filter('basic_filter')
+ ->class(\Vendor\Filter\BasicFilter::class)
+ ->enabled(true)
+ ->filter('complex_filter')
+ ->class(\Vendor\Filter\ComplexFilter::class)
+ ->enabled(false)
+ ->parameter('author', 'bob')
+ ->parameter('comments', [ '$gte' => 10 ])
+ ->parameter('tags', [ '$in' => [ 'foo', 'bar' ] ])
+ ;
+ }
+
.. note::
Unlike ORM, query parameters in MongoDB ODM may be non-scalar values. Since
@@ -332,12 +433,12 @@ following syntax:
database: db1
metadata_cache_driver: array
mappings:
- AcmeDemoBundle: ~
+ App: ~
dm2:
connection: conn2
database: db2
mappings:
- AcmeHelloBundle: ~
+ AnotherApp: ~
.. code-block:: xml
@@ -364,14 +465,42 @@ following syntax:
-
+
-
+
+ .. code-block:: php
+
+ use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->defaultDatabase('hello_' . param('kernel.environment'));
+ $config->defaultDocumentManager('dm2');
+ $config->defaultConnection('dm2');
+
+ $config->connection('conn1')
+ ->server('mongodb://localhost:27017');
+
+ $config->connection('conn2')
+ ->server('mongodb://localhost:27017');
+
+ $config->documentManager('dm1')
+ ->connection('conn1')
+ ->database('db1')
+ ->metadataCacheDriver('array')
+ ->mapping('App');
+
+ $config->documentManager('dm2')
+ ->connection('conn2')
+ ->database('db2')
+ ->mapping('AnotherApp');
+ };
+
Now you can retrieve the configured services connection services:
.. code-block:: php
@@ -419,12 +548,21 @@ string as a comma separated list and using ``replicaSet`` option.
+ .. code-block:: php
+
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->connection('default')
+ ->server('mongodb://mongodb-01:27017,mongodb-02:27017,mongodb-03:27017/?replicaSet=replSetName');
+ };
+
Where mongodb-01, mongodb-02 and mongodb-03 are the machine hostnames. You
can also use IP addresses if you prefer.
.. tip::
- Please refer to `Replica Sets`_ manual of MongoDB PHP Driver for futher details.
+ Please refer to `Replica Sets`_ manual of MongoDB PHP Driver for further details.
Using Authentication on a Database Level
@@ -472,6 +610,20 @@ Otherwise you will get a *auth failed* exception.
+ .. code-block:: php
+
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->connection('default')
+ ->server('mongodb://localhost:27017')
+ ->options([
+ 'username' => 'someuser',
+ 'password' => 'somepass',
+ 'authSource' => 'db_you_have_access_to',
+ ]);
+ };
+
Specifying a context service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -492,6 +644,20 @@ create a service that creates your logging context:
arguments:
- { ssl: { verify_expiry: true } }
+ .. code-block:: php
+
+ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
+
+ return static function (ContainerConfigurator $container): void {
+ $container->services()
+ ->set('app.mongodb.context_service', 'resource')
+ ->factory('stream_context_create')
+ ->args([
+ ['ssl' => ['verify_expiry' => true]],
+ ])
+ ;
+ };
+
Note: the ``class`` option is not used when creating the service, but has to be
provided for the service definition to be valid.
@@ -529,6 +695,18 @@ You can then use this service in your configuration:
+ .. code-block:: php
+
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->connection('default')
+ ->server('mongodb://localhost:27017')
+ ->driverOptions([
+ 'context' => 'app.mongodb.context_service',
+ ]);
+ };
+
Full Default Configuration
--------------------------
@@ -636,7 +814,6 @@ Full Default Configuration
hydrator-namespace="Hydrators"
proxy-dir="%kernel.cache_dir%/doctrine/odm/mongodb/Proxies"
proxy-namespace="Proxies"
- fixture-loader="Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader"
>
+ .. code-block:: php
+
+ use Symfony\Config\DoctrineMongodbConfig;
+
+ return static function (DoctrineMongodbConfig $config): void {
+ $config->autoGenerateHydratorClasses(0);
+ $config->autoGenerateProxyClasses(0);
+ $config->defaultConnection('');
+ $config->defaultDatabase('default');
+ $config->defaultDocumentManager('');
+ $config->hydratorDir('%kernel.cache_dir%/doctrine/odm/mongodb/Hydrators');
+ $config->hydratorNamespace('Hydrators');
+ $config->proxyDir('%kernel.cache_dir%/doctrine/odm/mongodb/Proxies');
+ $config->proxyNamespace('Proxies');
+
+ $config->documentManager('id')
+ ->connection('')
+ ->database('')
+ ->defaultDocumentRepositoryClass('')
+ ->defaultGridfsRepositoryClass('')
+ ->repositoryFactory('')
+ ->logging(true)
+ ->autoMapping(false)
+ ->metadataCacheDriver()
+ ->type(null)
+ ->class(null)
+ ->host(null)
+ ->port(null)
+ ->instanceClass(null)
+ ->mapping('name')
+ ->type('')
+ ->dir('')
+ ->prefix('')
+ ->alias('')
+ ->isBundle('')
+ ->profiler()
+ ->enabled(true)
+ ->pretty(false)
+ ;
+
+ $config->type('custom_type')
+ ->class('Fully\Qualified\Class\Name');
+
+ $config->connection('id')
+ ->server('mongodb://localhost')
+ ->driverOptions([
+ 'context' => null, // stream context to use for connection
+ ])
+ ->options([
+ 'authMechanism' => null,
+ 'connectTimeoutMS' => null,
+ 'db' => null,
+ 'authSource' => null,
+ 'journal' => null,
+ 'password' => null,
+ 'readPreference' => null,
+ 'readPreferenceTags' => null,
+ 'replicaSet' => null, // replica set name
+ 'socketTimeoutMS' => null,
+ 'ssl' => null,
+ 'tls' => null,
+ 'tlsAllowInvalidCertificates' => null,
+ 'tlsAllowInvalidHostnames' => null,
+ 'tlsCAFile' => null,
+ 'tlsCertificateKeyFile' => null,
+ 'tlsCertificateKeyFilePassword' => null,
+ 'tlsDisableCertificateRevocationCheck' => null,
+ 'tlsDisableOCSPEndpointCheck' => null,
+ 'tlsInsecure' => null,
+ 'username' => null,
+ 'retryReads' => null,
+ 'retryWrites' => null,
+ 'w' => null,
+ 'wTimeoutMS' => null,
+ ])
+ };
+
.. _`Custom types`: https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/current/reference/custom-mapping-types.html
.. _`define it as an environment variable`: https://symfony.com/doc/current/configuration.html#configuration-based-on-environment-variables
.. _`connection string`: https://docs.mongodb.com/manual/reference/connection-string/#urioption.authSource
diff --git a/docs/installation.rst b/docs/installation.rst
index 7521c465..54b8add0 100644
--- a/docs/installation.rst
+++ b/docs/installation.rst
@@ -35,7 +35,7 @@ To install DoctrineMongoDBBundle with Composer run the following command:
Enable the Bundle
-----------------
-Your bundle should be automatically enabled if you use Flex.
+The bundle should be automatically enabled if you use Flex.
Otherwise, you'll need to manually enable the bundle by adding the
following line in the ``config/bundles.php`` file of your project:
diff --git a/src/DependencyInjection/DoctrineMongoDBExtension.php b/src/DependencyInjection/DoctrineMongoDBExtension.php
index 18b3476b..ebd224da 100644
--- a/src/DependencyInjection/DoctrineMongoDBExtension.php
+++ b/src/DependencyInjection/DoctrineMongoDBExtension.php
@@ -462,14 +462,14 @@ private function normalizeDriverOptions(array $connection): array
*
* doctrine_mongodb:
* mappings:
- * MyBundle1: ~
- * MyBundle2: xml
- * MyBundle3: { type: attribute }
- * MyBundle4: { type: xml, dir: Resources/config/doctrine/mapping }
- * MyBundle5:
+ * App1: ~
+ * App2: xml
+ * App3: { type: attribute }
+ * App4: { type: xml, dir: config/doctrine/mapping }
+ * App5:
* type: xml
- * dir: [bundle-mappings1/, bundle-mappings2/]
- * alias: BundleAlias
+ * dir: [app-mappings1/, app-mappings2/]
+ * alias: AppAlias
* arbitrary_key:
* type: xml
* dir: %kernel.dir%/../src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents