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 1 commit
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
244 changes: 243 additions & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ Sample Configuration
</doctrine_mongodb:config>
</container>

.. code-block:: php

use function Symfony\Component\DependencyInjection\Loader\Configurator\env;
use Symfony\Config\DoctrineMongodbConfig;

return static function (DoctrineMongodbConfig $config): void {
$config->defaultConnection('default');
$config->connection('default')
->server(env('MONGODB_URL')->default('mongodb://localhost:27017')->resolve())
->options([]);

$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 +87,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 +139,29 @@ If you wish to use memcache to cache your metadata, you need to configure the
</doctrine_mongodb:config>
</container>

.. code-block:: php

use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Config\DoctrineMongodbConfig;

return static function (DoctrineMongodbConfig $config): void {
$config->defaultConnection('default');
$config->connection('default')
->server('mongodb://localhost:27017');

$config->documentManager('default')
->mapping('AcmeDemoBundle')
->filter('filter-name')
->class('Class\Example\Filter\ODM\ExampleFilter')
->enabled(true)
->metadataCacheDriver()
->type('memcached')
->class(MemcachedAdapter::class)
->host('localhost')
->port(11211)
->instanceClass(\Memcached::class)
;
};

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

.. code-block:: php

use Symfony\Config\DoctrineMongodbConfig;

return static function (DoctrineMongodbConfig $config): void {
$config->documentManager('default')
->mapping('MyBundle1')
->mapping('MyBundle2')
->type('xml')
->mapping('MyBundle3')
->type('attribute')
->dir(__DIR__.'/../src/Document')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the directories should be consistent with previous sections, so this should read Documents/

Copy link
Member Author

@GromNaN GromNaN Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the constants is one of the advantages of using the PHP configuration, I think it's better to show the full feature as it should be used in the documentation.

->mapping('MyBundle4')
->type('xml')
->dir(__DIR__.'/../config/doctrine/mapping')
malarzm marked this conversation as resolved.
Show resolved Hide resolved
->mapping('MyBundle5')
->type('xml')
->dir('my-bundle-mappings-dir')
->alias('BundleAlias')
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
->mapping('MyBundle6')
->type('xml')
->dir('%kernel.project_dir%/src/vendor/DoctrineExtensions/lib/DoctrineExtensions/Documents')
malarzm marked this conversation as resolved.
Show resolved Hide resolved
->prefix('DoctrineExtensions\\Documents\\')
->alias('DExt')
;
}


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

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

.. code-block:: php

use Fully\Qualified\Class\Name;
use Symfony\Config\DoctrineMongodbConfig;

return static function (DoctrineMongodbConfig $config): void {
$config->type('custom_type')
->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')
malarzm marked this conversation as resolved.
Show resolved Hide resolved
->enabled(true)
->filter('complex_filter')
->class('Vendor\Filter\ComplexFilter')
->enabled(true)
->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,35 @@ following syntax:
</doctrine_mongodb:config>
</container>

.. code-block:: php

use Symfony\Config\DoctrineMongodbConfig;

return static function (DoctrineMongodbConfig $config): void {
$config->defaultDatabase('hello_%kernel.environment%');
$config->defaultDocumentManager('dm2');
$config->defaultConnection('dm2');
$config->proxyNamespace('MongoDBODMProxies');
$config->autoGenerateProxyClasses(true);

$config->connection('conn1')
->server('mongodb://localhost:27017');

$config->connection('conn2')
->server('mongodb://localhost:27017');

$config->documentManager('dm1')
->metadataCacheDriver('array')
->connection('conn1')
->database('db1')
->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,6 +549,15 @@ 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.

Expand Down Expand Up @@ -472,6 +611,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 +645,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 +696,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 @@ -684,6 +863,69 @@ Full Default Configuration
</doctrine:config>
</container>

.. code-block:: php

use Symfony\Config\DoctrineConfig;

return static function (DoctrineConfig $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->fixtureLoader('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader');
malarzm marked this conversation as resolved.
Show resolved Hide resolved

$config->documentManager('id')
->connection('')
->database('')
->defaultDocumentRepositoryClass('')
->defaultGridfsRepositoryClass('')
->repositoryFactory('')
->logging(true)
->autoMapping(false)
->metadataCacheDriver()
->type('')
->class('')
->host('')
->port('')
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
->instanceClass('')
->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')
->options([
'authMechanism' => '',
'connectTimeoutMS' => '',
'db' => '',
'authSource' => '',
'journal' => '',
'password' => '',
'readPreference' => '',
'replicaSet' => '',
'socketTimeoutMS' => '',
'ssl' => '',
'username' => '',
'w' => '',
'wTimeoutMS' => '',
]);
};

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