Symfony 3.4.0 Could not find any fixture services to load

本小妞迷上赌 提交于 2019-11-27 17:20:34

问题


I am using Symfony 3.4.0, I try to load fixtures with:

php bin/console doctrine:fixtures:load

An error occurred while creating the data, what's wrong?


回答1:


This command looks for all services tagged with doctrine.fixture.orm.
There is two ways to fix this problem.

First one: any class that implements ORMFixtureInterface will automatically be registered with this tag.

<?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
    }
}

Second one: You need manually tag doctrine.fixture.orm to DataFixtures in sevice.yml configuration.

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']



回答2:


I tried @Alexander's solution but it's doesn't work for me.

I had resolved the same problem by adding the tag service to the class, Symfony doc on the services.yml file bundle:

BlogBundle/Resources/config/services.yml

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

My BlogBundle/DataFixtures/ORM/PostFixture.php class :

...
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
...

class PostFixture implements FixtureInterface
{
public function load(ObjectManager $manager)
    {
...
}
}

Source Inspiration : Synfony doc -> Service container -> The autoconfigure Option

Hope it'll an alternative solution




回答3:


Example for reusable bundle.

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
    }
}

in app/config/services.yml

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

append your fixtures data:

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



回答4:


~/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();
    }
}

The problem is solved it was necessary to add a service: (app/config/services.yml)

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



回答5:


In 4.0.1 I have to implement service configuration to show Symfony my DataFixtures folder:

in config/services.yaml

services:

    ...

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

if my class IMPLEMENTS FixtureInterface and without this config if it is EXTENDS Fixture




回答6:


use Doctrine\Bundle\FixturesBundle\Fixture

class ProductFixture extends Fixture implements FixtureInterface

see documentation: http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html




回答7:


After long research, found a solution. This work with :

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

First

  • Define your Fixture.
<?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();
    }
}

Next, define a service in the bundle's service.yml file or directly in "app/config/service.yml" (not recommanded)

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

Don't forget, just to be sure the following

composer du -o or composer dump-autoload -o

Try to execute your command now for load your data fixtures.




回答8:


I also had to update the app/AppKernel.php and added the following the the bundles array:

new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle()



回答9:


After read above comment, i found solution inside @GuRu answer's :

"Second one: You need manually tag doctrine.fixture.orm to DataFixtures in sevice.yml configuration".

Then implements ORMFixtureInterface in your fixtures class.

. in fact, we have to add additionnal configuration inside services.yml to solve it. Important to know, i notice this issue in version ~3.4 of symfony.

Best regard



来源:https://stackoverflow.com/questions/47613979/symfony-3-4-0-could-not-find-any-fixture-services-to-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!