Skip to content

Commit 48669b8

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Minor tweaks [DoctrineBundle] Add infos about disabling the Autocommit mode
2 parents de9e159 + d31c2d4 commit 48669b8

File tree

1 file changed

+101
-2
lines changed

1 file changed

+101
-2
lines changed

reference/configuration/doctrine.rst

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,104 @@ you can access it using the ``getConnection()`` method and the name of the conne
221221
}
222222
}
223223

224+
Disable Autocommit Mode
225+
~~~~~~~~~~~~~~~~~~~~~~~
226+
227+
By default, `autocommit`_ is enabled when using Doctrine DBAL. This means that
228+
each ``INSERT``, ``UPDATE``, or ``DELETE`` statement is immediately committed
229+
after it runs. You don't need to call ``commit()`` or ``rollback()`` because
230+
there's no open transaction.
231+
232+
You can disable autocommit to keep the connection inside a transaction until
233+
you explicitly call ``$connection->commit()`` or ``$connection->rollBack()``.
234+
Here's how to disable autocommit mode in DBAL:
235+
236+
.. configuration-block::
237+
238+
.. code-block:: yaml
239+
240+
doctrine:
241+
dbal:
242+
connections:
243+
default:
244+
options:
245+
# add this only if you're using DBAL with PDO:
246+
!php/const PDO::ATTR_AUTOCOMMIT: false
247+
248+
# this option disables auto-commit at the DBAL level:
249+
auto_commit: false
250+
251+
.. code-block:: xml
252+
253+
<?xml version="1.0" encoding="UTF-8" ?>
254+
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
255+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
256+
xmlns="http://symfony.com/schema/dic/services"
257+
xsi:schemaLocation="http://symfony.com/schema/dic/services
258+
https://symfony.com/schema/dic/services/services-1.0.xsd
259+
http://symfony.com/schema/dic/doctrine
260+
https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
261+
262+
<doctrine:config>
263+
<doctrine:dbal
264+
auto-commit="false"
265+
>
266+
<!-- add this only if you are using DBAL with PDO -->
267+
<doctrine:connection name="default">
268+
<doctrine:option key-type="constant" key="PDO::ATTR_AUTOCOMMIT">false</doctrine:option>
269+
</doctrine:connection>
270+
</doctrine:dbal>
271+
</doctrine:config>
272+
</container>
273+
274+
When using the `Doctrine Migrations Bundle`_, you need to register an additional
275+
listener to ensure that the final migration is committed properly:
276+
277+
.. configuration-block::
278+
279+
.. code-block:: yaml
280+
281+
# config/services.yaml
282+
services:
283+
Doctrine\Migrations\Event\Listeners\AutoCommitListener:
284+
tags:
285+
- name: doctrine.event_listener
286+
event: onMigrationsMigrated
287+
288+
.. code-block:: xml
289+
290+
<!-- config/services.xml -->
291+
<?xml version="1.0" encoding="UTF-8" ?>
292+
<container xmlns="http://symfony.com/schema/dic/services"
293+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
294+
xsi:schemaLocation="http://symfony.com/schema/dic/services
295+
https://symfony.com/schema/dic/services/services-1.0.xsd">
296+
297+
<services>
298+
<service id="Doctrine\Migrations\Event\Listeners\AutoCommitListener">
299+
<tag name="doctrine.event_listener" event="onMigrationsMigrated"/>
300+
</service>
301+
</services>
302+
</container>
303+
304+
.. code-block:: php
305+
306+
// config/services.php
307+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
308+
309+
use Doctrine\Migrations\Event\Listeners\AutoCommitListener;
310+
use Doctrine\Migrations\Events;
311+
312+
return function(ContainerConfigurator $container): void {
313+
$services = $container->services();
314+
315+
$services->set(AutoCommitListener::class)
316+
->tag('doctrine.event_listener', [
317+
'event' => Events::onMigrationsMigrated
318+
])
319+
;
320+
};
321+
224322
Doctrine ORM Configuration
225323
--------------------------
226324

@@ -659,6 +757,7 @@ Ensure your environment variables are correctly set in the ``.env.local`` or
659757
660758
This configuration secures your MySQL connection with SSL by specifying the paths to the required certificates.
661759

662-
663-
.. _DBAL documentation: https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/configuration.html
760+
.. _`autocommit`: https://en.wikipedia.org/wiki/Autocommit
761+
.. _`Doctrine Migrations Bundle`: https://github.com/doctrine/DoctrineMigrationsBundle
762+
.. _`DBAL documentation`: https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/configuration.html
664763
.. _`Doctrine Metadata Drivers`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/metadata-drivers.html

0 commit comments

Comments
 (0)