Symfony 3.4和Fixtures Bundle

时间:2017-12-13 12:15:45

标签: symfony fixture

我使用symfony 3.4.1和doctrine/doctrine-fixtures-bundle": "2.4.1

我试图运行灯具,但是当我尝试注入UserPasswordEncoderInterface来编码我的密码时,我收到了此消息。 https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#accessing-services-from-the-fixtures

  

致命错误:未捕获   Symfony \ Component \ Debug \ Exception \ FatalThrowableError:输入错误:太多   很少有功能论据   AppBundle \ DataFixtures \ DataFixtures :: __ construct(),0传入   /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php   在第210行和预期的1   /srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php:20   堆栈跟踪:

     

0 /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php(210):

     

的appbundle \ DataFixtures \ DataFixtures-> __构建体()

     

1 /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php(390):

     

学说\共同\ DataFixtures \装载机> createFixture('的appbundle \ DATAF ...&#39)

     

2 /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php(82):

     

学说\共同\ DataFixtures \装载机> loadFromIterator(对象(RecursiveIteratorIterator))

     

3 /srv/api-platform/vendor/doctrine/doctrine-fixtures-bundle/Command/LoadDataFixturesDoctrineCommand.php(102):

     

Doctrine \ Commo in   /srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php on   第20行

我的赛程:

<?php

namespace AppBundle\DataFixtures;

use AppBundle\Entity\TechnicalCenter;
use AppBundle\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class DataFixtures extends Fixture
{
    /** @var UserPasswordEncoderInterface $encoder */
    private $encoder;

    /**
     * DataFixtures constructor.
     * @param UserPasswordEncoderInterface $encoder
     */
    public function __construct(UserPasswordEncoderInterface $encoder)
    {
        $this->encoder = $encoder;
    }

    /**
     * Load data fixtures with the passed EntityManager
     *
     * @param ObjectManager $manager
     */
    public function load(ObjectManager $manager)
    {
        $technicalCenter = new TechnicalCenter();
        $technicalCenter->setName('Austerlitz');
        $manager->persist($technicalCenter);

        $admin = new User();
        $admin->setUsername('admin');
        $admin->setPassword($this->encoder->encodePassword($admin, 'admin'));
        $admin->setRoles(array('ROLE_ADMIN'));
        $admin->setTechnicalCenter($technicalCenter);
        $manager->persist($admin);

        $manager->flush();
    }
}

我的security.yml

security:
    encoders:
        AppBundle\Entity\User: bcrypt

1 个答案:

答案 0 :(得分:4)

在fixture中,我通过实现它并使用&#34; setContainer&#34;直接注入ContainerInterface。函数,例如:

<?php

namespace CoreBundle\DataFixtures\ORM;

use CoreBundle\Entity\Admin;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadAdminData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface {

    /**
     * @var ContainerInterface
     */
    private $container;

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

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $admin = new Admin();
        $admin->setUsername('Test');
        $plainPass = 'admin';
        $encoder = $this->container->get('security.password_encoder');
        $encodedPass = $encoder->encodePassword($admin, $plainPass);
        $admin->setPassword($encodedPass);
        $admin->setEmail('test@test.com');

        $manager->persist($admin);
        $manager->flush();
    }
    public function getOrder()
    {
        // the order in which fixtures will be loaded
        // the lower the number, the sooner that this fixture is loaded
        return 1;
    }
}