【问题标题】:Symfony & Doctrine MappedSuperClass - Fixtures FailSymfony 和 Doctrine MappedSuperClass - Fixtures 失败
【发布时间】:2018-10-28 19:51:49
【问题描述】:

我有这个映射的超类

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

这个位置类女巫继承了我的超级类:

/**
 * @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 location (name, street, zip_code, city, country) VALUES (?, ?, ?, ?, ?)' with params [“14549 梅耶高速公路\n森格切斯特,ME 42242”,空,空, 空,空]:

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

我在我的灯具中设置了我的街道属性,那么问题是什么?

【问题讨论】:

  • 奇怪 - 乍一看您的代码似乎很好,但异常消息中显示的失败插入中的第一个参数似乎是整个地址,包括街道名称、城市、州和邮政编码。并且所有单独的街道、邮编、城市和国家参数都是空的。这似乎是 Faker 库或配置的问题?
  • 我想知道default providers(包括地址)是否没有被加载?你有没有以任何方式配置 Faker?
  • @DarraghEnright 没什么特别的
  • 发生的事情似乎比上面显示的要多。我发现有趣的是第一个参数name 的值似乎是一个完整的地址。 Event::setLocation() 发生了什么?
  • 我会通过回显它来验证streetAddress 是否包含一个值。如果不是,我会添加地址提供者 ($faker->addProvider(new Faker\Provider\en_US\Address($faker));)。但是,由于这是一个默认提供程序并且您没有更改其配置,如果这是问题,我会感到惊讶。所以我很想看看Event::setLocation()Event::$location 的列定义。您是否在 Location 上定义了 __toString() 方法?

标签: php symfony doctrine-orm symfony4 fixtures


【解决方案1】:

谢谢你让我指出问题所在。我还在我的 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();
    }
}

【讨论】:

    猜你喜欢
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2015-04-16
    • 1970-01-01
    相关资源
    最近更新 更多