Search code examples
symfonysymfony4

Call to a member function encodePassword() on null, Symfony 4


I'm trying to save an encoded password to my entity by using the UserPasswordEncoderInterface class.

class UserFixture extends Fixture
{
private $encoder;

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

public function load(ObjectManager $manager)
{
    $user = new User();
    $user->setUsername('admin');
    $user->setPassword(
        $this->encoder->encodePassword($user, '0000')
    );
    $user->setEmail('[email protected]');
    $manager->persist($user);

    $manager->flush();
    }
}

But I'm getting the follwing error: Call to a member function encodePassword() on null. Can't find what I'm doing wrong here!


Solution

  • You should first check your namespace, but as @Cerad says, you mistyped __construct() which is the reason why your encoder is null.

    The right command to use in order to load your fixture properly might be :

    php bin/console doctrine:fixtures:load

    As the documentation recommends : https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html