5

I created an entity "Person" in Doctrine2, and I added to it an Adress entity, which is a value object (embeddable).

I want to allow a Person creation, without an Address, so I tag my embedded as "nullable = true". But on the other hand, my Address entity, if it exists, SHOULD contains at least some information (like city, zip code, etc...). So it has "nullable = false" attributes.

Address:
    type: embeddable
    fields:
        [...]
        city:
            type: string
            length: 255
            nullable: false

Person:
    type: entity
    table: null
    embedded:
        address:
            class: Address
            nullable: true

It seems that the "nullable = true" of the embedded object was not working. Do you know if it's a normal Doctrine behaviour ? Do I have to put all my embeddable attributes to nullable = true ?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Pete_Gore
  • 594
  • 1
  • 5
  • 20
  • 2
    Turns out that the embedded type does not have nullable defined. So it look like you will always get an Address value object. You will have to do some kind of test to see if it is empty or not. There is still not much documentation on embedded. I am going off of the xml xsd definition file: https://raw.githubusercontent.com/doctrine/doctrine2/master/doctrine-mapping.xsd Search for embedded. You can always use blank strings instead of null strings to get things to persist. – Cerad Nov 19 '15 at 19:34
  • I didn't know that xsd mapping file thanks. Indeed, I didn't find a nullable attribute for the embedded object, so you're certainly right... It seems that I have to define all the Address fields as nullable, and do manual checks in the constructor, for example... I'll try it out, thanks. – Pete_Gore Nov 20 '15 at 08:58

0 Answers0