Skip to content

Commit 4aa78dc

Browse files
committed
feat: update Doctrine documentation with PHP configuration examples
The commit introduces PHP configuration examples in the 'Doctrine' section for methods like connection(), dbal(), etc., along with the existing YAML examples. This is done to provide illustrative examples to users who prefer PHP configuration over YAML, and to ensure the document is consistent with Symfony's new practice of featuring both YAML and PHP particulars. # Conflicts: # reference/configuration/doctrine.rst
1 parent 784afc8 commit 4aa78dc

File tree

1 file changed

+146
-28
lines changed

1 file changed

+146
-28
lines changed

reference/configuration/doctrine.rst

Lines changed: 146 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ The following block shows all possible configuration keys:
100100
</doctrine:config>
101101
</container>
102102
103+
.. code-block:: php
104+
105+
use Symfony\Config\DoctrineConfig;
106+
107+
return static function (DoctrineConfig $doctrine): void {
108+
$dbal = $doctrine->dbal();
109+
110+
$dbal = $dbal
111+
->connection('default')
112+
->dbname('database')
113+
->host('localhost')
114+
->port(1234)
115+
->user('user')
116+
->password('secret')
117+
->driver('pdo_mysql')
118+
->url('mysql://db_user:db_password@127.0.0.1:3306/db_name') // if the url option is specified, it will override the above config
119+
->driverClass(App\DBAL\MyDatabaseDriver::class) // the DBAL driverClass option
120+
->option('foo', 'bar') // the DBAL driverOptions option
121+
->path('%kernel.project_dir%/var/data/data.sqlite')
122+
->memory(true)
123+
->unixSocket('/tmp/mysql.sock')
124+
->wrapperClass(App\DBAL\MyConnectionWrapper::class) // the DBAL wrapperClass option
125+
->charset('utf8mb4')
126+
->logging('%kernel.debug%')
127+
->platformService(App\DBAL\MyDatabasePlatformService::class)
128+
->serverVersion('8.0.37')
129+
->mappingType('enum', 'string')
130+
->type('custom', App\DBAL\MyCustomType::class);
131+
};
132+
103133
.. note::
104134

105135
The ``server_version`` option was added in Doctrine DBAL 2.5, which
@@ -125,7 +155,9 @@ The following block shows all possible configuration keys:
125155
If you want to configure multiple connections in YAML, put them under the
126156
``connections`` key and give them a unique name:
127157

128-
.. code-block:: yaml
158+
.. configuration-block::
159+
160+
.. code-block:: yaml
129161
130162
doctrine:
131163
dbal:
@@ -144,6 +176,29 @@ If you want to configure multiple connections in YAML, put them under the
144176
host: localhost
145177
server_version: '8.2.0'
146178

179+
.. code-block:: php
180+
181+
use Symfony\Config\DoctrineConfig;
182+
183+
return static function (DoctrineConfig $doctrine): void {
184+
$dbal = $doctrine->dbal();
185+
$dbal->defaultConnection('default');
186+
187+
$dbal->connection('default')
188+
->dbname('Symfony')
189+
->user('root')
190+
->password('null')
191+
->host('localhost')
192+
->serverVersion('8.0.37');
193+
194+
$dbal->connection('customer')
195+
->dbname('customer')
196+
->user('root')
197+
->password('null')
198+
->host('localhost')
199+
->serverVersion('8.2.0');
200+
};
201+
147202
The ``database_connection`` service always refers to the *default* connection,
148203
which is the first one defined or the one configured via the
149204
``default_connection`` parameter.
@@ -172,7 +227,9 @@ Doctrine ORM Configuration
172227
This following configuration example shows all the configuration defaults
173228
that the ORM resolves to:
174229

175-
.. code-block:: yaml
230+
.. configuration-block::
231+
232+
.. code-block:: yaml
176233
177234
doctrine:
178235
orm:
@@ -187,6 +244,32 @@ that the ORM resolves to:
187244
result_cache_driver: array
188245
naming_strategy: doctrine.orm.naming_strategy.default
189246

247+
.. code-block:: php
248+
249+
use Symfony\Config\DoctrineConfig;
250+
251+
return static function (DoctrineConfig $doctrine): void {
252+
$orm = $doctrine->orm();
253+
254+
$orm
255+
->entityManager('default')
256+
->connection('default')
257+
->autoMapping(true)
258+
->metadataCacheDriver()
259+
->type('array')
260+
->queryCacheDriver()
261+
->type('array')
262+
->resultCacheDriver()
263+
->type('array')
264+
->namingStrategy('doctrine.orm.naming_strategy.default');
265+
266+
$orm
267+
->autoGenerateProxyClasses(false) // the standard distribution overrides this to be true in debug, false otherwise
268+
->proxyNamespace('Proxies')
269+
->proxyDir('%kernel.cache_dir%/doctrine/orm/Proxies')
270+
->defaultEntityManager('default');
271+
};
272+
190273
There are lots of other configuration options that you can use to overwrite
191274
certain classes, but those are for very advanced use-cases only.
192275

@@ -230,35 +313,70 @@ Caching Drivers
230313
Use any of the existing :doc:`Symfony Cache </cache>` pools or define new pools
231314
to cache each of Doctrine ORM elements (queries, results, etc.):
232315

233-
.. code-block:: yaml
316+
.. configuration-block::
234317

235-
# config/packages/prod/doctrine.yaml
236-
framework:
237-
cache:
238-
pools:
239-
doctrine.result_cache_pool:
240-
adapter: cache.app
241-
doctrine.system_cache_pool:
242-
adapter: cache.system
318+
.. code-block:: yaml
243319
244-
doctrine:
245-
orm:
246-
# ...
247-
metadata_cache_driver:
248-
type: pool
249-
pool: doctrine.system_cache_pool
250-
query_cache_driver:
251-
type: pool
252-
pool: doctrine.system_cache_pool
253-
result_cache_driver:
254-
type: pool
255-
pool: doctrine.result_cache_pool
320+
# config/packages/prod/doctrine.yaml
321+
framework:
322+
cache:
323+
pools:
324+
doctrine.result_cache_pool:
325+
adapter: cache.app
326+
doctrine.system_cache_pool:
327+
adapter: cache.system
256328
257-
# in addition to Symfony Cache pools, you can also use the
258-
# 'type: service' option to use any service as the cache
259-
query_cache_driver:
260-
type: service
261-
id: App\ORM\MyCacheService
329+
doctrine:
330+
orm:
331+
# ...
332+
metadata_cache_driver:
333+
type: pool
334+
pool: doctrine.system_cache_pool
335+
query_cache_driver:
336+
type: pool
337+
pool: doctrine.system_cache_pool
338+
result_cache_driver:
339+
type: pool
340+
pool: doctrine.result_cache_pool
341+
342+
# in addition to Symfony Cache pools, you can also use the
343+
# 'type: service' option to use any service as the cache
344+
query_cache_driver:
345+
type: service
346+
id: App\ORM\MyCacheService
347+
348+
.. code-block:: php
349+
350+
use Symfony\Config\DoctrineConfig;
351+
use Symfony\Config\FrameworkConfig;w
352+
353+
return static function (FrameworkConfig $framework, DoctrineConfig $doctrine): void {
354+
$framework
355+
->cache()
356+
->pool('doctrine.result_cache_pool')
357+
->adapters('cache.app')
358+
->pool('doctrine.system_cache_pool')
359+
->adapters('cache.sytsem');
360+
361+
$doctrine->orm()
362+
// ...
363+
->entityManager('default')
364+
->metadataCacheDriver()
365+
->type('pool')
366+
->pool('doctrine.system_cache_pool')
367+
->queryCacheDriver()
368+
->type('pool')
369+
->pool('doctrine.system_cache_pool')
370+
->resultCacheDriver()
371+
->type('pool')
372+
->pool('doctrine.result_cache_pool')
373+
374+
// in addition to Symfony Cache pools, you can also use the
375+
// 'type: service' option to use any service as the cache
376+
->queryCacheDriver()
377+
->type('service')
378+
->id(App\ORM\MyCacheService::class);
379+
};
262380
263381
Mapping Configuration
264382
~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)