我正在使用symfony 3.4.1和 doctrine/doctrine-fixtures-bundle": "2.4.1
我'm trying to run fixtures, but I have this message when I'我试图注入 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-第210行的fixtures / lib / Doctrine / Common / DataFixtures / Loader.php和/srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php:20中预期的1个堆栈跟踪:0 / srv / api- platform / vendor / doctrine / data-fixtures / lib / Doctrine / Common / DataFixtures / Loader.php(210):AppBundle \ DataFixtures \ DataFixtures - > __ construct()1 / srv / api-platform / vendor / doctrine / data-fixture /lib/Doctrine/Common/DataFixtures/Loader.php(390):Doctrine \ Common \ DataFixtures \ Loader-> createFixture('AppBundle \ DataF ...')2 / srv / api-platform / vendor / doctrine / data- fixtures / lib / Doctrine / Common / DataFixtures / Loader.php(82):Doctrine \ Common \ DataFixtures \ Loader-> loadFromIterator(Object(RecursiveIteratorIterator))3 / srv / api-platform / vendor / doctrine / doctrine-fixtures-bundle /命令/ LoadDataFixturesDoctri neCommand.php(102):第20行的/srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php中的Doctrine \ Commo
我的赛程:
<?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 回答
在fixture中,我通过实现它并使用“setContainer”函数直接注入ContainerInterface,例如: