Symfony 4:防止在 DEV 环境中加载 Doctrine Fixtures
Symfony 4: Prevent Doctrine Fixtures from being loaded in DEV-Environment
如果你想防止数据库装置被意外地加载到错误的环境中,(可能)最好的方法是只在某些情况下激活DoctrineFixturesBundle
环境。
在 Symfony 3.4 之前,这是在 app/AppKernel.php
中完成的,如 https://symfony.com/doc/3.4/best_practices/business-logic.html#data-fixtures
中所述
如何在自动加载包的 Symfony 4 (Symfony Flex) 中实现这一点?
在 Symfony 4 中,这可以在 config/bundles.php
中配置,通过编辑行
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
见https://symfony.com/doc/4.1/best_practices/business-logic.html#data-fixtures
当你删除'dev' => true,
部分,然后尝试通过运行php bin/console doctrine:fixtures:load --env=dev
在DEV环境中加载灯具时,你将得到:
Error thrown while running command "'doctrine:fixtures:load' --env=dev". Message: "There are no commands defined in the "doctrine:fixtures" namespace.
但是,在测试环境中加载它们仍然有效:php bin/console doctrine:fixtures:load --env=test
如果你想防止数据库装置被意外地加载到错误的环境中,(可能)最好的方法是只在某些情况下激活DoctrineFixturesBundle
环境。
在 Symfony 3.4 之前,这是在 app/AppKernel.php
中完成的,如 https://symfony.com/doc/3.4/best_practices/business-logic.html#data-fixtures
如何在自动加载包的 Symfony 4 (Symfony Flex) 中实现这一点?
在 Symfony 4 中,这可以在 config/bundles.php
中配置,通过编辑行
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
见https://symfony.com/doc/4.1/best_practices/business-logic.html#data-fixtures
当你删除'dev' => true,
部分,然后尝试通过运行php bin/console doctrine:fixtures:load --env=dev
在DEV环境中加载灯具时,你将得到:
Error thrown while running command "'doctrine:fixtures:load' --env=dev". Message: "There are no commands defined in the "doctrine:fixtures" namespace.
但是,在测试环境中加载它们仍然有效:php bin/console doctrine:fixtures:load --env=test