Symfony Doctrine can't find fixtures to load

2019-03-01 02:25发布

I starting with Symfony (3.4) and I have problem with load fixture.
When I execute php bin/console doctrine:fixtures:load then I get message:

In LoadDataFixturesDoctrineCommand.php line 95:
  Could not find any fixture services to load.

There is my code:

~/src/AppBundle/DataFixtures/ORM/LoadUserData.php

namespace AppBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadUserData implements FixtureInterface, ContainerAwareInterface {

    private $container;

    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager)
    {
        $user = new User();
        $user->setLogin('admin');
        $user->setEmail('admin@admin.admin');
        $encoder = $this->container->get('security.password_encoder');
        $password = $encoder->encodePassword($user, '123qwe');
        $user->setPassword($password);

        $manager->persist();
        $manager->flush();
    }

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
}

~/app/config/services.yml

    parameters:

    services:
        _defaults:
            autowire: true
            autoconfigure: true
            public: false

        AppBundle\:
            resource: '../../src/AppBundle/*'
            exclude: '../../src/AppBundle/{Entity,Repository,Tests}'

        AppBundle\Controller\:
            resource: '../../src/AppBundle/Controller'
            public: true
            tags: ['controller.service_arguments']

Thanks.

2条回答
老娘就宠你
2楼-- · 2019-03-01 02:54

Depending on what version of fixtures you use you should extend/implement different classes. If the version is >= 3.0 then

extend Fixture (use Doctrine\Bundle\FixturesBundle\Fixture;)

If < 3.0

implements FixtureInterface, ContainerAwareInterface
查看更多
淡お忘
3楼-- · 2019-03-01 03:05

In LoadDataFixturesDoctrineCommand.php line 95:

Could not find any fixture services to load.

A Resolution: for symfony 3.4.* and doctrine-fixtures-bundle 3.0 ~/src/YC/PlatformBundle/DataFixtures/LoadCategory.php

namespace YC\PlatformBundle\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;

use Doctrine\Common\Persistence\ObjectManager;

use YC\PlatformBundle\Entity\Categorie;


class LoadCategory extends Fixture
{

  public function load(ObjectManager $manager)
  {
    $names = array(
        'Développement web',
        'Développement mobile',
        'Graphisme',
        'Integration',
        'Reseau'
    );

    foreach ($names as $name) {
        $category = new Categorie();
        $category->setName($name);
        $manager->persist($category);
    }

    $manager->flush();
  }

}

~/src/YC/PlatformBundle/DataFixtures/LoadCategory.php

YC\PlatformBundle\DataFixtures:

    resource: '%kernel.project_dir%/src/YC/PlatformBundle/DataFixtures'
    tags: ['doctrine.fixture.orm']
查看更多
登录 后发表回答