我的问题出在我的实体之间有一个相互依赖的关系这一事实。我的实体链接与实体用户有关系。当我尝试使用UserManagerInterface加载用户时,它将返回 NULL 。 这是我的AppFixtures类:
<?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 FOS\UserBundle\Model\UserManagerInterface;
use Faker;
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)
{
$faker = Faker\Factory::create();
$user = $this->userManager->createUser();
$user->setUsername('admin');
$user->setEmail('admin@kss.tk');
$user->setPlainPassword('admin');
$user->setEnabled(true);
$user->setRoles(array('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();
}
}
我不想每次都刷新数据库,我想使用现有的用户,所以我可以添加固定装置。现在它会引发错误:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin' for key 'UNIQ_1483A5E992FC23A8'