Symfony 和 Doctrine MappedSuperClass - Fixtures 失败
Symfony & Doctrine MappedSuperClass - Fixtures Fail
我有这个 Mapped Super Class
/**
* @ORM\MappedSuperclass()
*/
abstract class Address
{
/**
* @ORM\Column(type="string", length=255)
*/
protected $street;
/**
* @ORM\Column(type="string", length=10)
*/
protected $zipCode;
/**
* @ORM\Column(type="string", length=255)
*/
protected $city;
/**
* @ORM\Column(type="string", length=255)
*/
protected $country;
public function getStreet(): ?string
{
return $this->street;
}
public function setStreet(string $street): self
{
$this->street = $street;
return $this;
}
public function getZipCode(): ?string
{
return $this->zipCode;
}
public function setZipCode(string $zipCode): self
{
$this->zipCode = $zipCode;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(string $country): self
{
$this->country = $country;
return $this;
}
}
而这个位置class继承了我的超级class:
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\LocationRepository")
*/
class Location extends Address
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
我想添加一些固定装置:
class LocationFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
$events = $manager->getRepository(Event::class)->findAll();
$faker = Factory::create();
foreach ($events as $event) {
$location = new Location();
$location->setName($faker->streetName);
$location->setStreet($faker->streetAddress);
$location->setCity($faker->city);
$location->setCountry($faker->country);
$location->setZipCode($faker->postcode);
$event->setLocation($location);
$manager->persist($event);
}
$manager->flush();
}
}
但是那些失败并显示此错误消息:
An exception occurred while executing 'INSERT INTO location (name,
street, zip_code, city, country) VALUES (?, ?, ?, ?, ?)' with params
["14549 Mayer Freeway\nSengerchester, ME 42242", null, null,
null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'street'
cannot be null
我在灯具中设置了街道属性,这是什么问题?
谢谢你让我指出了问题所在。我还在我的 EventFixture 中设置了一个位置。正是这些人失败了。
我把所有东西都放在了我的 Event fixture 中并且它起作用了
class EventFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
$faker = Factory::create();
for ($i = 0; $i < 10; $i++) {
$event = new Event();
$event->setName($faker->name);
$event->setDescription($faker->text);
$dateStart = $faker->dateTime;
$event->setStartAt($dateStart);
$event->setFinishAt($dateStart->add(new \DateInterval('P2D')));
$event->setPrice($faker->numberBetween(20, 200));
$event->setType(EventType::PHOTOGRAPHY);
$event->setInstagram($faker->firstName);
$location = new Location();
$location->setName($faker->streetName);
$location->setStreet($faker->streetAddress);
$location->setCity($faker->city);
$location->setCountry($faker->country);
$location->setZipCode($faker->postcode);
$manager->persist($location);
$event->setLocation($location);
$manager->persist($event);
}
$manager->flush();
}
}
我有这个 Mapped Super Class
/**
* @ORM\MappedSuperclass()
*/
abstract class Address
{
/**
* @ORM\Column(type="string", length=255)
*/
protected $street;
/**
* @ORM\Column(type="string", length=10)
*/
protected $zipCode;
/**
* @ORM\Column(type="string", length=255)
*/
protected $city;
/**
* @ORM\Column(type="string", length=255)
*/
protected $country;
public function getStreet(): ?string
{
return $this->street;
}
public function setStreet(string $street): self
{
$this->street = $street;
return $this;
}
public function getZipCode(): ?string
{
return $this->zipCode;
}
public function setZipCode(string $zipCode): self
{
$this->zipCode = $zipCode;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getCountry(): ?string
{
return $this->country;
}
public function setCountry(string $country): self
{
$this->country = $country;
return $this;
}
}
而这个位置class继承了我的超级class:
/**
* @ApiResource()
* @ORM\Entity(repositoryClass="App\Repository\LocationRepository")
*/
class Location extends Address
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
我想添加一些固定装置:
class LocationFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
$events = $manager->getRepository(Event::class)->findAll();
$faker = Factory::create();
foreach ($events as $event) {
$location = new Location();
$location->setName($faker->streetName);
$location->setStreet($faker->streetAddress);
$location->setCity($faker->city);
$location->setCountry($faker->country);
$location->setZipCode($faker->postcode);
$event->setLocation($location);
$manager->persist($event);
}
$manager->flush();
}
}
但是那些失败并显示此错误消息:
An exception occurred while executing 'INSERT INTO location (name, street, zip_code, city, country) VALUES (?, ?, ?, ?, ?)' with params ["14549 Mayer Freeway\nSengerchester, ME 42242", null, null, null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'street' cannot be null
我在灯具中设置了街道属性,这是什么问题?
谢谢你让我指出了问题所在。我还在我的 EventFixture 中设置了一个位置。正是这些人失败了。
我把所有东西都放在了我的 Event fixture 中并且它起作用了
class EventFixtures extends Fixture
{
public function load(ObjectManager $manager)
{
$faker = Factory::create();
for ($i = 0; $i < 10; $i++) {
$event = new Event();
$event->setName($faker->name);
$event->setDescription($faker->text);
$dateStart = $faker->dateTime;
$event->setStartAt($dateStart);
$event->setFinishAt($dateStart->add(new \DateInterval('P2D')));
$event->setPrice($faker->numberBetween(20, 200));
$event->setType(EventType::PHOTOGRAPHY);
$event->setInstagram($faker->firstName);
$location = new Location();
$location->setName($faker->streetName);
$location->setStreet($faker->streetAddress);
$location->setCity($faker->city);
$location->setCountry($faker->country);
$location->setZipCode($faker->postcode);
$manager->persist($location);
$event->setLocation($location);
$manager->persist($event);
}
$manager->flush();
}
}