Skip to content

Nullable value objects and empty state of embeddables #12

@edigu

Description

@edigu

There are some discussions exists about this subject on github. Learned that doctrine manages the nullable embeddable objects different than I think. Seems like having a literally empty instance for both nullable and non-nullable cases is preferred in ORM land. This comment from the discussions below nicely summaries the situation:

by setting nullable as TRUE you are making it nullable: you are not accepting the idea of invalid data being hydrated. "nullable" and "inconsistent" are very different concepts, and SQL DBMSs without struct-like fields aren't able to deal with this sort of data-integrity problem in a decent way, therefore it's simply better to avoid the problem in first place by putting a strict limitation instead of building more foot-gunning tools - ocramius

Some facts that I found and understand so far:

  • Wishing a nullable value object to utilize in our entities doesn't means that we cannot have partial (incomplete) VO objects on runtime. Preventing a possible write operation to the database with an incomplete value object seems like responsibility of the developer.
  • Nullable value objects may have a method to tell outside if the instance is empty (all fields are null) or not. I'll create another PR for this. (This was a really bad idea. I'll revert it soon. Having an isEmpty() method in instance is not way to go) Anyway, still an empty value object instance is NOT EQUALS to null.
  • Object hydration must (or should) be done outside the ORM. With or without an ORM, application should know how to hydrate domain entities from various input formats and sources (serialized, json, native array, xml..).
  • When utilizing embeddables, seems like having an empty value object instead of managing nulls is more straightforward. Official documentation is also recommends this here.

Since all attributes of the value objects in this library marked as nullable, I'm planning to create a PR to allow empty object construction by changing all constructors from

public function __construct($hex) { } : Color

to

public function __construct($hex = null) { } : Color

I would like to hear opinions from others on this subject. Please feel free to share your thoughts.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions