Symfony Doctrine无法找到要加载的灯具

时间:2017-12-05 19:45:27

标签: php symfony doctrine-orm orm doctrine

我从Symfony(3.4)开始,我遇到了装载夹具的问题 当我执行php bin/console doctrine:fixtures:load时,我收到消息:

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

有我的代码:

〜/ 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;
    }
}

〜/应用/配置/ 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']

感谢。

2 个答案:

答案 0 :(得分:5)

根据您使用的灯具版本,您应该扩展/实现不同的类。 如果版本是> = 3.0那么

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

如果< 3.0

implements FixtureInterface, ContainerAwareInterface

答案 1 :(得分:0)

LoadDataFixturesDoctrineCommand.php第95行:

  

无法找到要加载的任何灯具服务。

分辨率:对于symfony 3.4。*和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']