Symfony/FOSUserBundle: Load an existing User from database in Fixtures

258 Views Asked by At

My problem comes from the fact that i have a relation in my entities that depends on each other. My entity Link has a relation with an entity User. When I try to load the User using UserManagerInterface it returns NULL. Here is My AppFixtures class:

<?php

namespace App\DataFixtures;

use App\Component\Localization;
use App\Entity\Link;
use App\Entity\LinkClick;
use App\Entity\LinkView;
use App\Entity\Visitor;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Faker;
use FOS\UserBundle\Model\UserManagerInterface;
use PUGX\Shortid\Shortid;

class AppFixtures extends Fixture
{
    private $userManager;

    private $localisation;

    public function __construct(UserManagerInterface $userManager, Localization $localization)
    {
        $this->userManager = $userManager;

        $faker = Faker\Factory::create();
        $localization->setParameters($localization::GEO_PLUGIN, $faker->ipv4);
        $localization->localize();
        $this->localisation = $localization->getResult();
    }

    public function load(ObjectManager $manager): void
    {
        $faker = Faker\Factory::create();
        $user  = $this->userManager->createUser();
        $user->setUsername('admin');
        $user->setEmail('[email protected]');
        $user->setPlainPassword('admin');
        $user->setEnabled(true);
        $user->setRoles(['ROLE_ADMIN']);
        $this->userManager->updateUser($user);
        $links    = [];
        $visitors = [];
        for ($i = 0; $i < 100; ++$i) {
            $link = new Link();
            $link->setUrl($faker->url);
            $link->setSlug(Shortid::generate(12, 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01', true));
            $link->setCreatedAt($faker->dateTime);
            $link->setUser($user);
            $links[] = $link;
            $manager->persist($link);
        }

        for ($i = 0; $i < 10000; ++$i) {
            $visitor = new Visitor();
            $visitor->setIp($this->localisation->geoplugin_request);
            $visitor->setCountryCode($this->localisation->geoplugin_countryCode);
            $visitor->setCountryName($this->localisation->geoplugin_countryName);
            $visitors[] = $visitor;
            $manager->persist($visitor);
        }

        for ($i = 0; $i < 10000; ++$i) {
            $linkView = new LinkView();
            $linkView->setDate($faker->dateTimeBetween('-1 years', 'now'));
            $linkView->setVisitor($visitors[rand(0, 999)]);
            $linkView->setLink($links[rand(0, 99)]);
            $manager->persist($linkView);
        }

        for ($i = 0; $i < 10000; ++$i) {
            $linkClick = new LinkClick();
            $linkClick->setDate($faker->dateTimeBetween('-1 years', 'now'));
            $linkClick->setVisitor($visitors[rand(0, 999)]);
            $linkClick->setLink($links[rand(0, 99)]);
            $manager->persist($linkClick);
        }

        $manager->flush();
    }
}

I dont want to flush the database everytime, i want to use and existing user, so i can append fixtures. Right now it throws an error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin' for key 'UNIQ_1483A5E992FC23A8'
0

There are 0 best solutions below