I'm trying to work on a symfony project. I created my file fixtures I want to add a key for my table
to make club.$key
to send to my database and create several club
class ClubFixtures extends Fixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$this->loadClubs($manager);
}
/**
* @param ObjectManager $manager
* @throws BadMethodCallException
*/
private function loadClubs(ObjectManager $manager)
{
foreach ($this->getClubData() as [ $establishmentName, $address, $code]) {
$club = new Club();
$club->setEstablishmentName($establishmentName);
$club->setAddress($address);
$club->setCode($code);
$club->addUsers($this->getReference('user1'));
$club->addGames($this->getReference('game1'));
$manager->persist($club);
$manager->flush();
$this->addReference('club1', $club);
}
}
private function getClubData(): array
{
return [
// $clubData = [$establishmentName, $address, $code, $user, $games];
['establishmentName1', '60 rue de belleville', 342],
['establishmentName2', '65 rue de jaures', 543],
['establishmentName3', '80 rue de stalingrad', 987]
];
}
/**
* Get the order of this fixture
*
* @return integer
*/
public function getOrder()
{
return 1;
}
I am a beginner in this domain.
答案 0 :(得分:1)
使用foreach function时出错。
所以,你的代码应该是这样的:
foreach ($this->getClubData() as $key => list($establishmentName, $address, $code)) {
$club = new Club();
$club->setEstablishmentName($establishmentName);
$club->setAddress($address);
$club->setCode($code);
$club->addUsers($this->getReference('user1'));
$club->addGames($this->getReference('game1'));
$manager->persist($club);
$manager->flush();
$this->addReference('club'.$key, $club);
}
此外,检查your logs非常有用。通过这种方式,您可以更快地找到原因并进行修复,或者只是将其复制到此处。
答案 1 :(得分:0)
我找到了想要激发灵感的解决方案: 用户俱乐部协会
类UserFixtures扩展了Fixture实现的OrderedFixtureInterface {
private $passwordEncoder;
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}
public function load(ObjectManager $manager)
{
$this->loadUsers($manager);
}
/**
* @param ObjectManager $manager
* @throws \Doctrine\Common\DataFixtures\BadMethodCallException
*/
private function loadUsers(ObjectManager $manager)
{
foreach ($this->getUserData() as $key => [$name, $username, $email, $birthdate, $password, $address, $userCode,
$roles, $token, $service, $picture, $pictureName, $status]) {
$user = new User();
$user->setName($name);
$user->setUsername($username);
$user->setEmail($email);
$user->setBirthdate($birthdate);
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
$user->setAddress($address);
$user->setUserCode($userCode);
$user->setRoles($roles);
$user->setToken($token);
$user->setService($service);
$user->setPicture($picture);
$user->setPictureName($pictureName);
$user->setStatus($status);
$user->setClub($this->getReference('club1'));
$manager->persist($user);
$this->addReference('user' . $key, $user);
}
$manager->flush();
}
private function getUserData(): array
{
return [
['Jane Doe', 'jane_admin', 'jane_admin@symfony.com', new \DateTime('2008-10-12'), 'test', '45 rue de tourtille', 139879, 'ROLE_ADMIN', 'jdijididi43gf', 'admin', 'image1', 'mon profil', 1],
['Tom Doe', 'tom_admin', 'tom_admin@symfony.com', new \DateTime('2009-10-11 '), 'test', '46 rue de tourtille', 135879, 'ROLE_SUPER_ADMIN', 'di43gfjdijididi', 'super_admin', 'image2', 'profil', 1],
['John Doe', 'john_user', 'john_user@symfony.com', new \DateTime('2010-09-10 '), 'test', '43 rue de tourtille', 139797, 'ROLE_USER', 'jdijidikaoli43gf', 'marketing', 'image3', 'ma phot', 1]
];
}
/**
* Get the order of this fixture
*
* @return integer
*/
public function getOrder()
{
return 2;
}
类ClubFixtures扩展了Fixture实现的OrderedFixtureInterface {
public function load(ObjectManager $manager)
{
$this->loadClubs($manager);
}
/**
* @param ObjectManager $manager
* @throws BadMethodCallException
*/
private function loadClubs(ObjectManager $manager)
{
foreach ($this->getClubData() as $key => [$establishmentName, $address, $code]) {
$club = new Club();
$club->setEstablishmentName($establishmentName);
$club->setAddress($address);
$club->setCode($code);
$manager->persist($club);
$this->addReference('club'.$key, $club);
}
$manager->flush();
}
private function getClubData(): array
{
return [
// $clubData = [$establishmentName, $address, $code];
['establishmentName1', '60 rue de belleville', 342],
['establishmentName2', '65 rue de jaures', 543],
['establishmentName3', '80 rue de stalingrad', 987]
];
}
/**
* Get the order of this fixture
*
* @return integer
*/
public function getOrder()
{
return 1;
}