Let's say I've got two Doctrine entities, Person
and Company
. Both have an address
field which accepts an Address value object. As per business rules, Company::Address
is required while Person::Address
can be null.
Doctrine 2.5 proposes the Embeddable type, which was apparently built with value objects in mind and, indeed, I see it as a perfect solution for my case.
However, there's one thing I can't do: declare that Person::Address
is nullable while Company::Address
is not. A boolean nullable
attribute exists for the Embeddable's fields themselves, but of course this applies to every entity the Address is embedded in.
Does anybody know if I'm missing something, or if this is due to a technical limitation, if there's a workaround, etc. ? Right now the only solution I see is to declare all Embeddable fields as nullable: true
and handle the constraint in my code.
Does anybody know if I'm missing something
Nullable embeddables are not supported in Doctrine 2. They are expected to make it to version 3.
if there's a workaround
The solution "is to NOT use embeddables there, and [...] replace fields with embeddables [manually]" (@Ocramius)
Example:
class Product
{
private $sale_price_amount;
private $sale_price_currency;
public function getSalePrice(): ?SalePrice
{
if (is_null($this->sale_price_currency)
|| is_null($this->sale_price_amount)
) {
return null;
}
return new SalePrice(
$this->sale_price_currency,
$this->sale_price_amount
);
}
}
(Snippet by Harrison Brown)