【问题标题】:Symfony 3.4.0 Could not find any fixture services to loadSymfony 3.4.0 找不到要加载的任何夹具服务
【发布时间】:2018-05-16 18:35:01
【问题描述】:

我正在使用 Symfony 3.4.0,我尝试使用以下方式加载固定装置:

php bin/console doctrine:fixtures:load

创建数据时出错,怎么回事?

【问题讨论】:

  • 你的灯具在哪里?哪个目录?可以显示代码
  • 您的灯具应该位于例如在src\AppBundle\DataFixtures\ORM\MyFixture.php
  • 补丁:~/dev/domain.lan/src/ProductBundle/DataFixtures/ORM/ProductFixture.php 网站根目录:~/dev/domain.lan/
  • Symfony 3.4 中也出现了同样的错误。在 Symfony 3.3 中一切正常。

标签: symfony doctrine fixtures symfony-3.4


【解决方案1】:

阅读上述评论后,我在@GuRu 答案中找到了解决方案:

第二个:你需要在sevice.yml配置中手动标记doctrine.fixture.orm到DataFixtures”。

然后在你的fixtures类中实现ORMFixtureInterface。

。事实上,我们必须在 services.yml 中添加额外的配置来解决它。 重要的是要知道,我在 symfony 的 ~3.4 版本中注意到了这个问题。

最好的尊重

【讨论】:

  • 这个答案带来了什么新的东西?您所说的一切都已在答案和评论中完成
【解决方案2】:

我还必须更新 app/AppKernel.php 并在 bundles 数组中添加以下内容:

new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle()

【讨论】:

    【解决方案3】:

    可重用包的示例。

    src/Acme/Bundle/UserBundle/DataFixtures/ORM/DataFixtures.php

    <?php 
    namespace Acme\Bundle\UserBundle\DataFixtures\ORM;
    
    use Doctrine\Bundle\FixturesBundle\Fixture;
    use Doctrine\Common\Persistence\ObjectManager;
    
    class DataFixtures extends Fixture
    {
        /**
         * Load data fixtures with the passed EntityManager
         *
         * @param ObjectManager $manager
         */
        public function load(ObjectManager $manager)
        {
           #your code
        }
    }
    

    在 app/config/services.yml 中

    Acme\Bundle\UserBundle\DataFixtures\:
         resource: '../../src/Acme/Bundle/UserBundle/DataFixtures/'
    

    附加您的灯具数据:

    php bin/console doctrine:fixtures:load --append
    

    【讨论】:

      【解决方案4】:

      经过长时间的研究,找到了解决方案。 这项工作与:

      • doctrine/doctrine-fixtures-bundle: ^3.0,
      • Symfony ^3.3

      第一

      • 定义您的夹具。
      <?php
      namespace Where\MyFixtureBundle\FileFolder\IsLocated;
      
      use Doctrine\Common\DataFixtures\FixtureInterface;
      use Doctrine\Common\Persistence\ObjectManager;
      use Nao\UserBundle\Entity\User;
      
      class LoadData implements FixtureInterface
      {
          /**
           * Load data fixtures with the passed EntityManager
           *
           * @param ObjectManager $manager
           */
          public function load(ObjectManager $manager){
              $object = new Entity();
              $object->setFoo(bar)
              ...
      
              $manager->persist($object);
      
              $manager->flush();
          }
      }
      

      接下来,在 bundle 的 service.yml 文件 或直接在 “app/config/service.yml”(不推荐)

      # Fixtures service
      your_service.name:
          class: Full\Namespce\With\TheClassFixtureName
          tags: [doctrine.fixture.orm] <-- important
      

      不要忘记,只是为了确保以下内容

      composer du -o or composer dump-autoload -o
      

      现在尝试执行您的命令以加载您的数据夹具。

      【讨论】:

        【解决方案5】:

        此命令查找所有带有doctrine.fixture.orm 标记的服务。
        有两种方法可以解决此问题。

        第一个:任何实现ORMFixtureInterface的类都会自动注册到这个标签。

        <?php
        
        namespace AppBundle\DataFixtures\ORM;
        
        
        use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
        use Doctrine\Common\Persistence\ObjectManager;
        
        class LoadFixtures implements ORMFixtureInterface
        {
            public function load(ObjectManager $manager)
            {
                #your code
            }
        }
        

        第二个:需要在sevice.yml配置中手动将doctrine.fixture.orm标记为DataFixtures

        services:
            ...
        
            # makes classes in src/AppBundle/DataFixtures available to be used as services
            # and have a tag that allows actions to type-hint services
            AppBundle\DataFixtures\:
                resource: '../../src/AppBundle/DataFixtures'
                tags: ['doctrine.fixture.orm']
        

        【讨论】:

        • 我花了很多时间尝试注册我的夹具数据类。所以,这个接口“ORMFixtureInterface”的实现——让我很开心:) 谢谢你,先生!
        • Symfony 也这么说!引用:“如果您使用默认服务配置,任何实现 ORMFixtureInterface 的类都将自动使用此标签注册。” symfony.com/doc/current/bundles/DoctrineFixturesBundle/…
        【解决方案6】:

        在 4.0.1 中,我必须实现服务配置以向 Symfony 显示我的 DataFixtures 文件夹:

        在 config/services.yaml 中

        services:
        
            ...
        
            App\DataFixtures\:
                resource: '../src/DataFixtures'
                tags: [doctrine.fixture.orm]
        

        如果我的班级实现了 FixtureInterface 并且没有此配置,如果它是 EXTENDS Fixture

        【讨论】:

        • @Dimitry,最好用这个resource: '%kernel.project_dir%/src/DataFixtures'
        【解决方案7】:

        使用 Doctrine\Bundle\FixturesBundle\Fixture

        类 ProductFixture 扩展了 Fixture 实现了 FixtureInterface

        查看文档:http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html

        【讨论】:

          【解决方案8】:

          我尝试了@Alexander 的解决方案,但它对我不起作用。

          我通过在 services.yml 文件包上的类中添加标签服务 Symfony doc 解决了同样的问题:

          BlogBundle/Resources/config/services.yml

          Services:
          ...
          # Fixtures services
              BlogBundle\DataFixtures\ORM\PostFixture:
                  tags: [doctrine.fixture.orm]
          ...
          

          我的BlogBundle/DataFixtures/ORM/PostFixture.php 班级:

          ...
          use Doctrine\Common\DataFixtures\FixtureInterface;
          use Doctrine\Common\Persistence\ObjectManager;
          ...
          
          class PostFixture implements FixtureInterface
          {
          public function load(ObjectManager $manager)
              {
          ...
          }
          }
          

          来源灵感:Synfony doc -> Service container -> The autoconfigure Option

          希望这是一个替代解决方案

          【讨论】:

          • 此解决方案运行良好。当我将教义装置从 v2.4.1 升级到 3.0.2 时,我开始收到此错误。
          【解决方案9】:

          ~/dev/domain.lan/src/ProductBundle/DataFixtures/ORM/ProductF‌​ixture.php

          <?php
          
          namespace ProductBundle\DataFixtures\ORM;
          
          use Doctrine\Bundle\FixturesBundle\FixtureInterface;
          use Doctrine\Common\Persistence\ObjectManager;
          use ProductBundle\Entity\Product;
          
          class ProductFixture implements FixtureInterface
          {
          
              public function load(ObjectManager $manager)
              {
                 // create 20 products! Bam!
                 for ($i = 0; $i < 20; $i++) {
                     $product = new Product();
                     $product->setName('Product name' . $i);
                     $manager->persist($product);
                 }
          
                 $manager->flush();
              }
          }
          

          问题解决了需要添加服务:(app/config/services.yml)

          services:
              # Product service
              ProductBundle\:
                  resource: '../../src/ProductBundle/*'
                  exclude: '../../src/ProductBundle/{Entity,Repository,Tests}'
          

          【讨论】:

          • 我是你应该将它添加到 services.yml 捆绑文件例如:~/dev/domain.lan/src/ProductBundle/Resoueces/config/services.yml
          猜你喜欢
          • 2019-03-29
          • 2018-12-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多