Additional Display Doctrine OneToOne

I am trying to create an optional OneToOne mapping in Doctrine.

I have a table with all cities and postal codes (this table should not be changed), and I have a table with addresses and the city displayed. But sometimes I don’t want to add the City to my address at the beginning (maybe it will be later). But when I do not add the City to the address, storing in the address gives me a Reflection exception, because there is no object of type "null", which must be a City object.

I do not want to add an empty mailbox to the database every time, because nothing should be added or deleted to the city table.

Any suggestions? Or what am I missing?

class Address{ /** * @OneToOne(targetEntity="City") * @JoinColumn(name="city_id", referencedColumnName="id") */ private $city = ''; 

Possible solutions that I considered:

  • Create an empty city object in db and always assign it to newly created address objects (this can cause a lot of overhead).
  • Create a ManyToMany relationship with an array of cities, so zero or more cities can be added (I can limit many cities in my Address object), but then I need a mapping table ...
+7
source share
1 answer

Just add nullable=true to the @JoinColumn annotation

+16
source

All Articles