0
votes

I have this 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;
    }
}

And this Location class witch inherit my Super 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;
    }
}

I wanted to add some fixtures:

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

But those one fail with this error message:

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

I setting my street attribut in my fixture so what is the problem?

1
Strange - at first glance your code seems fine but the first param in that failed insert shown in the exception message seems to be the entire address, incliding streetname, city, state and zip code. And all of the individual street, zip, city and country params are null. This seems to be an issue with, or configuration of, the Faker library?Darragh Enright
I wonder if the default providers (including address) are not being loaded? Have you configured Faker in any way?Darragh Enright
@DarraghEnright No nothing specialKevin
There appears to be more happening than what is shown above. I find it interesting that the value of the first param name appears to be a full address. What's happening in Event::setLocation()?Darragh Enright
I would verify that streetAddress contains a value by echoing it out. If it's not I'd add the address provider ($faker->addProvider(new Faker\Provider\en_US\Address($faker));). However, since this is a default provider and you haven't changed its configuration I'd be surprised if this were the issue. So I'd be very interested to see Event::setLocation() and the column definition for Event::$location. Have you defined a __toString() method on Location?Darragh Enright

1 Answers

0
votes

Thanks you made me point out what was the problem. I was also setting a location in my EventFixture. And it was this those who was failing.

I putted everything in my Event fixture and it worked

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