Symfony和Doctrine MappedSuperClass-固定装置失败

时间:2018-10-28 19:51:49

标签: php symfony doctrine-orm symfony4 fixtures

我有这个映射的超级班

/**
 * @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;
    }
}

这个Location类的女巫继承了我的Super类:

/**
 * @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();
    }
}

但是那些失败并显示此错误消息:

  

执行'INSERT INTO位置(名称,   街道,邮递区号,城市,国家/地区)VALUES(?,?,?,?,?)   [“ 14549 Mayer Freeway \ nSengerchester,ME 42242”,null,null,   null,null]:

     

SQLSTATE [23000]:违反完整性约束:1048列'street'   不能为空

我在设备中设置了街道属性,这是什么问题?

1 个答案:

答案 0 :(得分:0)

谢谢你让我指出了问题所在。我还在EventFixture中设置了位置。而正是那些失败的人。

我将所有内容都放入了Event固定装置,并且一切正常

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();
    }
}