0

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('admin@kss.tk');
        $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'
Mikhail Prosalov
  • 4,155
  • 4
  • 29
  • 41
Salim Ben Aissa
  • 435
  • 1
  • 6
  • 21
  • I think this error appear because there is already a username with this value 'admin'. According to this documentation [sharing-objects-between-fixtures](https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures), you can share Objects between Fixtures. So you can define a user fixture and link fixture, you define the priority between them to force the user fixture to be load before link fixture ( [loading-the-fixture-files-in-order](https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#loading-the-fixture-files-in-order) ) – William Bridge Mar 17 '19 at 08:50
  • The problem i dont want to recreate the user everytime, but if i do what you suggested nothing will change, i will still can't make an append loading. – Salim Ben Aissa Mar 17 '19 at 16:37
  • 1- Do you really need to use this line : ```$this->userManager->updateUser($user);``` ? 2- According to this answer [load-container-and-entitymanager-within-a-fixture-in-symfony2](https://stackoverflow.com/questions/31493548/load-container-and-entitymanager-within-a-fixture-in-symfony2), you can try it with symfony 4, by checking first if the user with 'admin' value for username already exist in the database. if no you create this user (without flushing). All this it's to avoid the Integrity constraint violation error. – William Bridge Mar 18 '19 at 02:13

0 Answers0