__construct期望一个参数,什么也没有传入(Fixtures)

时间:2019-03-28 14:07:04

标签: symfony fixtures

我正在为我的项目(symfony 3.4)创建Somme灯具,一切似乎正确,找不到问题,我想念什么? 如果有人有一个好主意:)

use AppBundle\Entity\User;

use AppBundle\Entity\Task;

use Doctrine\Bundle\FixturesBundle\Fixture;

use Doctrine\Common\Persistence\ObjectManager;

use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class AppFixtures extends Fixture
{
private $encoder;

public function __construct(UserPasswordEncoderInterface $encoder)
{
    $this->encoder = $encoder;
}

/**
 * @param ObjectManager $manager
 */
public function load(ObjectManager $manager)
{
    // Create user with role ROLE_ADMIN

    $user_admin = new User();

    $user_admin->setUsername( 'admin' );

    $user_admin->setPassword($this->encoder->encodePassword($user_admin, 'password' ));

    $user_admin->setEmail( 'admin@mail.fr' );

    $user_admin->setRoles( 'ROLE_ADMIN' );

    $manager->persist( $user_admin );

1 个答案:

答案 0 :(得分:0)

您应该实现ContainerAwareInterface并从容器中获取编码器,请参见此处的答案:How to inject service into Symfony 2 Data Fixtures?