Skip to content

Commit d75b53b

Browse files
Informations about disabling the autocommit mode
1 parent cc48c92 commit d75b53b

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

reference/configuration/doctrine.rst

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,91 @@ you can access it using the ``getConnection()`` method and the name of the conne
166166
}
167167
}
168168

169+
Disabling the Autocommit mode
170+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171+
172+
If you want to disable the `Autocommit`_ mode, you need to update your DBAL configuration as follows:
173+
174+
.. configuration-block::
175+
176+
.. code-block:: yaml
177+
178+
doctrine:
179+
dbal:
180+
connections:
181+
default:
182+
options:
183+
# Only if you're using DBAL with PDO:
184+
!php/const PDO::ATTR_AUTOCOMMIT: false
185+
186+
# This line disables auto-commit at the DBAL level:
187+
auto_commit: false
188+
189+
.. code-block:: xml
190+
<?xml version="1.0" encoding="UTF-8" ?>
191+
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
192+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
193+
xmlns="http://symfony.com/schema/dic/services"
194+
xsi:schemaLocation="http://symfony.com/schema/dic/services
195+
https://symfony.com/schema/dic/services/services-1.0.xsd>
196+
<doctrine:config>
197+
<doctrine:dbal
198+
auto-commit="false"
199+
>
200+
<!-- Only if you are using DBAL with PDO -->
201+
<doctrine:option key-type="constant" key="PDO::ATTR_AUTOCOMMIT">false</doctrine:option>
202+
</doctrine:dbal>
203+
</doctrine:config>
204+
</container>
205+
206+
207+
If you are managing your database migrations using the `Doctrine Migrations Bundle`_, you must also register a listener to ensure that the last migration is properly commited:
208+
209+
.. configuration-block::
210+
211+
.. code-block:: yaml
212+
213+
# config/services.yaml
214+
services:
215+
Doctrine\Migrations\Event\Listeners\AutoCommitListener:
216+
tags:
217+
- name: doctrine.event_listener
218+
event: !php/const Doctrine\Migrations\Events::onMigrationsMigrated
219+
220+
.. code-block:: xml
221+
222+
<!-- config/services.xml -->
223+
<?xml version="1.0" encoding="UTF-8" ?>
224+
<container xmlns="http://symfony.com/schema/dic/services"
225+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
226+
xsi:schemaLocation="http://symfony.com/schema/dic/services
227+
https://symfony.com/schema/dic/services/services-1.0.xsd">
228+
229+
<services>
230+
<service id="Doctrine\Migrations\Event\Listeners\AutoCommitListener">
231+
<tag name="doctrine.event_listener" event="onMigrationsMigrated" />
232+
</service>
233+
</services>
234+
</container>
235+
236+
.. code-block:: php
237+
238+
// config/services.php
239+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
240+
241+
use Doctrine\Migrations\Event\Listeners\AutoCommitListener;
242+
use Doctrine\Migrations\Events;
243+
244+
return function(ContainerConfigurator $container): void {
245+
$services = $container->services();
246+
247+
$services->set(AutoCommitListener::class)
248+
->tag('doctrine.event_listener', [
249+
'event' => Events::onMigrationsMigrated
250+
])
251+
;
252+
};
253+
169254
Doctrine ORM Configuration
170255
--------------------------
171256
@@ -544,6 +629,7 @@ Ensure your environment variables are correctly set in the ``.env.local`` or
544629
545630
This configuration secures your MySQL connection with SSL by specifying the paths to the required certificates.
546631
547-
632+
.. _Autocommit: https://en.wikipedia.org/wiki/Autocommit
633+
.. _Doctrine Migrations Bundle: https://github.com/doctrine/DoctrineMigrationsBundle
548634
.. _DBAL documentation: https://www.doctrine-project.org/projects/doctrine-dbal/en/current/reference/configuration.html
549635
.. _`Doctrine Metadata Drivers`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/metadata-drivers.html

0 commit comments

Comments
 (0)