【问题标题】:Symfony using different doctrine fixture for different environments (prod, test, dev)Symfony 为不同的环境(产品、测试、开发)使用不同的学说夹具
【发布时间】:2019-02-27 20:21:58
【问题描述】:

您好,我对使用 dataFixtures 有疑问,我想在 prod、dev、test 环境中使用fixture。我曾尝试使用--fixtures 选项,但它是一个未找到的选项。 如何使用我想要的文件在命令行上加载我的灯具?

是否可以使用doctrine:fixtures:load 命令的--env 选项来做到这一点?

我有固定装置

  • App/DataFixtures/Prod
  • App/DataFixtures/Dev
  • 应用/DataFixtures/测试

我正在使用 symfony 3.4 谢谢你的帮助

【问题讨论】:

    标签: symfony doctrine-orm fixtures


    【解决方案1】:

    不幸的是,--fixtures 选项已在 DoctrineFixturesBundle 3.0 中删除,该问题即将通过使用“sets”的不同approach 解决。该解决方案似乎已实现,但尚未合并到 DoctrineFixturesBundle 主控中。

    我建议当时耐心一些。

    编辑:如何使用环境来解决这个问题:

    正如您在评论中所问的,您确实可以使用 env 选项来解决这个问题,如下所示:

    首先,您应该创建一个抽象的 Fixture 类,该类应该存在于您的 DataFixtures 目录中,并注入容器,以便您可以从内核中获取当前环境:

    namespace App\DataFixtures;
    
    use Doctrine\Common\DataFixtures\FixtureInterface;
    use Doctrine\Common\Persistence\ObjectManager;
    use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    abstract class AbstractFixture implements ContainerAwareInterface, FixtureInterface
    {
        protected $container;
    
        public function setContainer(ContainerInterface $container = null)
        {
            $this->container = $container;
        }
    
        public function load(ObjectManager $manager)
        {
        
            $kernel = $this->container->get('kernel');
    
            if (in_array($kernel->getEnvironment(), $this->getEnvironments())) {
                $this->doLoad($manager);
            }
        }
    
        abstract protected function doLoad(ObjectManager $manager);
    
        abstract protected function getEnvironments();
    }
    

    然后你应该为每个环境(prod、test、dev)扩展这个抽象的 Fixture 类,如下所示(示例仅适用于 prod):

    namespace App\DataFixtures;
    
    use Doctrine\Common\Persistence\ObjectManager;
    
    class ProdFixture extends AbstractFixture
    {
    
        protected function doLoad(ObjectManager $manager)
        {
            // load what you need to load for prod environment 
        }
    
        protected function getEnvironments()
        {
            return ['prod'];
        }
     }
    

    这些ProdFixtureTestFixtureDevFixture 等类也应该存在于您的 DataFixtures 目录中。

    使用此设置,每次运行带有--env 选项的doctrine:fixtures:load 命令时,所有Fixture 类都会初始加载(AbstractFixture 类除外),但只有在getEnvironments() 中设置了相应环境的Fixture 类才会真正执行.

    【讨论】:

    • 感谢您的回答,我在选项列表中看到了 --env 我不知道如何使用它,如果它是一个解决方案?
    • 编辑了我的答案以解释如何使用 --env 选项。
    • 我已经重现了相同的方法,但是当我使用教义:fixtures:load --env=prod 时,我有一个错误“没有在“doctrine:fixtures”命名空间中定义的命令。 ' 如果我删除 --env 选项,命令工作:(
    • 对不起,我认为这是因为该捆绑包已注册开发并在 appKernel 上进行测试
    • 一个小问题:) 在 --env=dev 它加载 DevFixture 和 ProdFixture 正常,但我在 dev 文件中有一些参考到 prod 文件上的夹具 ($this->getReference()) 失败
    【解决方案2】:

    Symfony 在夹具包中引入了“组”的概念。例如,您现在可以按环境对灯具进行分组。

    https://symfony.com/blog/new-in-fixturesbundle-group-your-fixtures

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      相关资源
      最近更新 更多