Symfony 3.4 и комплекты приспособлений

Я использую Symfony 3.4.1 с doctrine/doctrine-fixtures-bundle": "2.4.1

Я пытаюсь запустить приборы, но у меня появляется это сообщение, когда я пытаюсь ввести UserPasswordEncoderInterface закодировать мой пароль. https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html

Неустранимая ошибка: Uncaught 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):

Доктрина \ Common \ DataFixtures \ Loader-> createFixture ('AppBundle \ DataF...')

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

Доктрина \ Common \ DataFixtures \ Loader-> loadFromIterator (Object (RecursiveIteratorIterator))

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

Doctrine \ Commo в /srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php в строке 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 ответ

Решение

В приспособлениях я внедряю непосредственно ContainerInterface, реализуя его и используя функцию "setContainer", например:

<?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;
    }
}
Другие вопросы по тегам