2017-12-13 12 views
0

내가 비품을 실행하기 위해 노력하고있어 doctrine/doctrine-fixtures-bundle": "2.4.1심포니 3.4 및 설비 번들

와 심포니 3.4.1을 사용하고 있습니다,하지만 난 내 비밀 번호를 인코딩하기 위해 UserPasswordEncoderInterface을 주입하기 위해 노력하고있어이 메시지를 가지고있다. https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#accessing-services-from-the-fixtures

Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Too few arguments to function AppBundle\DataFixtures\DataFixtures::__construct(), 0 passed in /srv/api-platform/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Loader.php on line 210 and exactly 1 expected in /srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php:20 Stack trace:

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-fixtures/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/Command/LoadDataFixturesDoctrineCommand.php(102):

Doctrine\Commo in /srv/api-platform/src/AppBundle/DataFixtures/ORM/DataFixtures.php on line 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 
+0

표준 서비스 구성을 사용하고 있습니까? 즉, services.yml 파일이 Symfony 3.4의 표준 장소에 있습니까? – ASOlivieri

+0

그렇지 않은 경우 (또는 그렇더라도) 서비스로 등록기를 등록하고 인코더를 삽입하려고 했습니까? – ASOlivieri

답변

2

나는 그것을 구현하고 exemple를 들어, "setContainer"기능을 사용하여 직접 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('[email protected]'); 

     $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; 
    } 
}