简体   繁体   中英

Symfony 6 fixtures UserPasswordHasherInterface

How can we use UserPasswordHasherInterface in a fixture to create users in Symfony 6?

//App/DataFixtures/AppFixtures.php

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class AppFixtures extends Fixture
{
    private $manager;

    public function load(ObjectManager $manager, UserPasswordHasherInterface $userPasswordHasherInterface): void
    {
        $this->manager = $manager;

        $user = new User();
        $user->setEmail("test@example.com");
        //$user->setPassword("test_pass");
        $user->setPassword(
            $userPasswordHasherInterface->hashPassword(
                $user, "test_pass"
            )
        );

        $user->setFirstName("Fixture First 1");
        $user->setLastName("Fixture Last 1");

        $this->manager->persist($user);
        $this->manager->flush();
    }
}

Error returned

Fatal error: Declaration of App\DataFixtures\AppFixtures::load(Doctrine\Persistence\ObjectManager $manager, Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $userPasswordH
asherInterface): void must be compatible with Doctrine\Common\DataFixtures\FixtureInterface::load(Doctrine\Persistence\ObjectManager $manager) in C:\wamp64\www\example.com\src\DataFixtures
\AppFixtures.php on line 14
Symfony\Component\ErrorHandler\Error\FatalError {#138
  #message: "Compile Error: Declaration of App\DataFixtures\AppFixtures::load(Doctrine\Persistence\ObjectManager $manager, Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface
 $userPasswordHasherInterface): void must be compatible with Doctrine\Common\DataFixtures\FixtureInterface::load(Doctrine\Persistence\ObjectManager $manager)"
  #code: 0
  #file: "C:\wamp64\www\example.com\src\DataFixtures\AppFixtures.php"
  #line: 14
  -error: array:4 [
    "type" => 64
    "message" => "Declaration of App\DataFixtures\AppFixtures::load(Doctrine\Persistence\ObjectManager $manager, Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface $userPass
wordHasherInterface): void must be compatible with Doctrine\Common\DataFixtures\FixtureInterface::load(Doctrine\Persistence\ObjectManager $manager)"
    "file" => "C:\wamp64\www\example.com\src\DataFixtures\AppFixtures.php"
    "line" => 14
  ]
}

You need to inject UserPasswordHasherInterface in a constructor , like this:

//App/DataFixtures/AppFixtures.php

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class AppFixtures extends Fixture
{
    private $userPasswordHasherInterface;

    public function __construct (UserPasswordHasherInterface $userPasswordHasherInterface) 
    {
        $this->userPasswordHasherInterface = $userPasswordHasherInterface;
    }

    public function load(ObjectManager $manager): void
    {
        $user = new User();
        $user->setEmail("test@example.com");
        //$user->setPassword("test_pass");
        $user->setPassword(
            $this->userPasswordHasherInterface->hashPassword(
                $user, "test_pass"
            )
        );

        $user->setFirstName("Fixture First 1");
        $user->setLastName("Fixture Last 1");

        $manager->persist($user);
        $manager->flush();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM