1

I want generate faker users with symfony with Datafixtures and I hash users password with "UserPasswordHasher" but when I run the command " php bin/console doctrine:fixtures:load" I have this critical error : "Message: "Object of class Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher could not be converted to string"

Somebody had this problem?

Thank you for yours answers and I'm sorry for my bad english

This is my AppFixtures.php file:

<?php

namespace App\DataFixtures;

use App\Entity\Author;
use App\Entity\Book;
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 $hasher;

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

    public function load(ObjectManager $manager): void
    {
        //Création d'un user normal
        $user = new User();
        $user->setEmail('[email protected]');
        $user->setRoles(['ROLE_USER']);
        $hashPassword = $this->hasher->hashPassword($user, 'password');
        $user->setPassword($hashPassword);
        $manager->persist($user);

        //Création d'un admin
        $userAdmin = new User();
        $userAdmin->setEmail('[email protected]');
        $userAdmin->setRoles(['ROLE_ADMIN']);
        $hashPassword = $this->hasher->hashPassword($userAdmin, 'password');
        $user->setPassword($hashPassword);
        $manager->persist($userAdmin);

        //Création des auteurs
        $listAuthors = [];
        for ($i = 0; $i < 10; $i++){
            $author = new Author();
            $author->setLastname('Nom' . $i);
            $author->setFirstname('Prénom' . $i);
            $manager->persist($author);
            // On sauvegarde l'auteur crée dans un tableau
            $listAuthors[]= $author;
        }

        //Création des livres
        for ($i = 0; $i < 20; $i++){
            $book = new Book();
            $book->setTitle('Titre'.$i);
            $book->setCoverText('Quatrieme de couverture numéro:'.$i);
            // On lie le livre à un auteur pris au hasard dans le tableau des auteurs
            $book->setAuthor($listAuthors[array_rand($listAuthors)]);
            $manager->persist($book);
        }

        $manager->flush();
    }
}
4
  • 3
    I see $this->$hasher and $this->hasher, simple typo in your constructor? Commented Jan 23, 2023 at 22:31
  • Thank you my friend , I’m stupid I had lost my time during 2 hours for a syntax error. Commented Jan 24, 2023 at 8:50
  • Yeah I wasn't sure, but I guess its trying to convert the object into a string for the variable name, Do you use an IDE? Something like PHP storm is pretty good at helping spot these things. Commented Jan 24, 2023 at 22:07
  • Yes I use Visual Studio Code but he didn't tell me any problems Commented Jan 24, 2023 at 22:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.