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?
name
appears to be a full address. What's happening inEvent::setLocation()
? – Darragh EnrightstreetAddress
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 seeEvent::setLocation()
and the column definition forEvent::$location
. Have you defined a__toString()
method onLocation
? – Darragh Enright