Symfony 6 固定装置 UserPasswordHasherInterface
Symfony 6 fixtures UserPasswordHasherInterface
我们如何在 fixture 中使用 UserPasswordHasherInterface 在 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();
}
}
返回错误
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
]
}
您需要在 constructor
中注入 UserPasswordHasherInterface
,如下所示:
//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();
}
}
我们如何在 fixture 中使用 UserPasswordHasherInterface 在 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();
}
}
返回错误
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
]
}
您需要在 constructor
中注入 UserPasswordHasherInterface
,如下所示:
//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();
}
}