Declare Doctrine Embeddable Nullable or Not

Say I have two Doctrine objects, Personand Company. Both have a field addressthat accepts an address value object. In accordance with business rules is required Company::Address, and Person::Addressmay be zero.

Doctrine 2.5 offers an embeddable type that seems to have been built with value objects and, in fact, I find this the ideal solution for my case.

However, I can’t do anything: declare what Person::Addressis valid, but Company::Addressnot. The boolean attribute nullableexists for the nested fields themselves, but, of course, this applies to all entities in which the address is nested.

Does anyone know if I missed something, or if it is due to a technical limitation, if there is a workaround, etc.? Right now the only solution I see is to declare all nested fields as nullable: trueand handle the restriction in my code.

+4
source share
1 answer

Does anyone know if I missed something

Nullable embeddables are not supported in Doctrine 2. They must do this before version 3.

if there is a workaround

Solution "DO NOT use nested attachments there" and [...] replace fields with attached files [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
        );
    }
}

(fragment Harrison Brown )

+4

Source: https://habr.com/ru/post/1679630/


All Articles