Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration example with PHP format #834

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 257 additions & 3 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ Sample Configuration
</doctrine_mongodb:config>
</container>

.. 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('AcmeDemoBundle')
->filter('filter-name')
->class(\Class\Example\Filter\ODM\ExampleFilter::class)
->enabled(true)
->metadataCacheDriver('array'); // array, service, apcu, memcached, redis
};
}


.. tip::

If each environment requires a different MongoDB connection URI, you can
Expand All @@ -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::
Expand Down Expand Up @@ -118,6 +140,28 @@ If you wish to use memcache to cache your metadata, you need to configure the
</doctrine_mongodb:config>
</container>

.. 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('AcmeDemoBundle')
->metadataCacheDriver()
->type('memcached')
->class(MemcachedAdapter::class)
->host('localhost')
->port(11211)
->instanceClass(\Memcached::class)
;
};

Mapping Configuration
---------------------
Expand Down Expand Up @@ -214,6 +258,35 @@ The following configuration shows a bunch of mapping examples:
</doctrine_mongodb:config>
</container>

.. 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('MyBundle1')
->mapping('MyBundle2')
->type('xml')
->mapping('MyBundle3')
->type('attribute')
->dir('Documents/')
->mapping('MyBundle4')
->type('xml')
->dir('Resources/config/doctrine/mapping')
->mapping('MyBundle5')
->type('xml')
->dir('my-bundle-mappings-dir')
->alias('BundleAlias')
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
->mapping('doctrine_extensions')
->type('xml')
->dir(param('kernel.project_dir') . '/src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents')
->prefix('DoctrineExtensions\\Documents\\')
->alias('DExt')
;
}


Custom Types
------------

Expand Down Expand Up @@ -244,6 +317,16 @@ your documents.
</doctrine_mongodb:config>
</container>

.. code-block:: php

use Symfony\Config\DoctrineMongodbConfig;

return static function (DoctrineMongodbConfig $config): void {
$config->type('custom_type')
->class(\Fully\Qualified\Class\Name::class)
;
}

Filters
-------

Expand Down Expand Up @@ -298,6 +381,24 @@ Filters may be registered with a document manager by using the following syntax:
</doctrine:mongodb>
</container>

.. 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
Expand Down Expand Up @@ -372,6 +473,34 @@ following syntax:
</doctrine_mongodb:config>
</container>

.. 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('AcmeDemoBundle');

$config->documentManager('dm2')
->connection('conn2')
->database('db2')
->mapping('AcmeHelloBundle');
};

Now you can retrieve the configured services connection services:

.. code-block:: php
Expand Down Expand Up @@ -419,12 +548,21 @@ string as a comma separated list and using ``replicaSet`` option.
</doctrine:mongodb>
</container>

.. 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
Expand Down Expand Up @@ -472,6 +610,20 @@ Otherwise you will get a *auth failed* exception.
</doctrine:mongodb>
</container>

.. 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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -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.

Expand Down Expand Up @@ -529,6 +695,18 @@ You can then use this service in your configuration:
</doctrine:mongodb>
</container>

.. 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
--------------------------

Expand Down Expand Up @@ -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"
>
<doctrine:document-manager id="id"
connection=""
Expand Down Expand Up @@ -684,6 +861,83 @@ Full Default Configuration
</doctrine:config>
</container>

.. 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
Expand Down