@@ -42,9 +42,12 @@ application:
4242 # config/packages/doctrine_migrations.yaml
4343
4444 doctrine_migrations :
45+ # Whether to enable fetching migrations from the service container.
46+ enable_service_migrations : false
47+
4548 # List of namespace/path pairs to search for migrations, at least one required
4649 migrations_paths :
47- ' App\Migrations ' : ' %kernel.project_dir%/src/App '
50+ ' App\Migrations ' : ' %kernel.project_dir%/src/Migrations '
4851 ' AnotherApp\Migrations ' : ' /path/to/other/migrations'
4952 ' SomeBundle\Migrations ' : ' @SomeBundle/Migrations'
5053
@@ -234,11 +237,12 @@ Doctrine will then assume that this migration has already been run and will igno
234237Migration Dependencies
235238----------------------
236239
237- Migrations can have dependencies on external services (such as geolocation, mailer, data processing services...) that
238- can be used to have more powerful migrations. Those dependencies are not automatically injected into your migrations
239- but need to be injected using custom migrations factories .
240+ Migrations can have dependencies on external services (such as geolocation, mailer or data processing services) to
241+ enable more advanced behavior. To inject dependencies into your migrations, you must enable loading migrations from
242+ the service container and register the migrations as services .
240243
241- Here is an example on how to inject the service container into your migrations:
244+ If you are using Symfony's default service configuration, migration services are registered automatically
245+ once placed in the ``src `` directory:
242246
243247.. configuration-block ::
244248
@@ -247,62 +251,71 @@ Here is an example on how to inject the service container into your migrations:
247251 # config/packages/doctrine_migrations.yaml
248252
249253 doctrine_migrations :
250- services :
251- ' Doctrine\Migrations\Version\MigrationFactory ' : ' App\Migrations\Factory\MigrationFactoryDecorator'
254+ enable_service_migrations : true
255+ migrations_paths :
256+ ' App\Migrations ' : ' %kernel.project_dir%/src/Migrations'
257+
258+
259+ If you are not using the default configuration, register your migration classes manually and make sure they are
260+ discoverable by the autoloader. If autoconfiguration is disabled, tag them manually with
261+ the ``doctrine_migrations.migration `` tag:
262+
263+ .. configuration-block ::
264+
265+ .. code-block :: yaml
252266
253267 # config/services.yaml
254268
255269 services :
256- App\Migrations\Factory\MigrationFactoryDecorator :
257- decorates : ' doctrine.migrations.migrations_factory'
258- arguments : ['@.inner', '@service_container']
270+ DoctrineMigrations\Version20180605025653 :
271+ tags : [ 'doctrine_migrations.migration' ]
272+ arguments :
273+ $myService : ' @App\Services\MyService'
259274
260275
261- .. code-block :: php
276+ The connection and logger services are injected automatically through bindings. You may specify them explicitly
277+ if needed:
262278
263- declare(strict_types=1);
279+ .. configuration-block ::
264280
265- namespace App\Migrations\Factory;
281+ .. code-block :: yaml
266282
267- use Doctrine\Migrations\AbstractMigration;
268- use Doctrine\Migrations\Version\MigrationFactory;
269- use Symfony\Component\DependencyInjection\ContainerAwareInterface;
270- use Symfony\Component\DependencyInjection\ContainerInterface;
283+ # config/services.yaml
271284
272- class MigrationFactoryDecorator implements MigrationFactory
273- {
274- private $migrationFactory;
275- private $container;
285+ services :
286+ DoctrineMigrations\Version20180605025653 :
287+ tags : [ 'doctrine_migrations.migration' ]
288+ arguments :
289+ - ' @doctrine.migrations.connection'
290+ - ' @doctrine.migrations.logger'
291+ - ' @App\Services\MyService'
276292
277- public function __construct(MigrationFactory $migrationFactory, ContainerInterface $container)
278- {
279- $this->migrationFactory = $migrationFactory;
280- $this->container = $container;
281- }
282293
283- public function createVersion(string $migrationClassName): AbstractMigration
284- {
285- $instance = $this->migrationFactory->createVersion($migrationClassName);
294+ Then override the constructor in your migration class and add your dependencies:
286295
287- if ($instance instanceof ContainerAwareInterface) {
288- $instance->setContainer($this->container);
289- }
296+ .. code-block :: php
290297
291- return $instance;
292- }
293- }
298+ declare(strict_types=1);
294299
300+ namespace App\Migrations;
295301
296- .. tip ::
302+ use App\Services\MyService;
303+ use Doctrine\DBAL\Schema\Schema;
304+ use Doctrine\Migrations\AbstractMigration;
305+
306+ final class Version20180605025653 extends AbstractMigration
307+ {
308+ private MyService $myService;
297309
298- If your migration class implements the interface `` Symfony\Component\DependencyInjection\ContainerAwareInterface ``
299- this bundle will automatically inject the default symfony container into your migration class
300- (this because the `` MigrationFactoryDecorator `` shown in this example is the default migration factory used by this bundle).
310+ public function __construct(Connection $connection, LoggerInterface $logger, MyService $myService)
311+ {
312+ parent::__construct($connection, $logger);
301313
302- .. caution ::
314+ $this->myService = $myService;
315+ }
303316
304- The interface `` Symfony\Component\DependencyInjection\ContainerAwareInterface `` has been deprecated in Symfony 6.4 and
305- removed in 7.0. If you use this version or newer, there is currently no way to inject the service container into migrations.
317+ // ...
318+ }
306319
307320
308321 Generating Migrations Automatically
0 commit comments