Embedded Value (Embeddables) with Doctrine >= 2.5.*

The latest stable Doctrine release is currently version 2.5 and it comes with support for mapping Value Objects, thereby removing the need to do this yourself as in Doctrine 2.4. Since December 2015, Doctrine also has support for nested embeddables. The support is not 100 percent, but it's high enough to give it a try. In case it doesn't work for your scenario, take a look at the next section. For official documentation, check the Doctrine Embeddables reference. This option, if implemented correctly, is definitely the one we recommend most. It would be the simplest, most elegant solution, that also provides search support in your DQL queries.

Because the Product, Money, and Currency classes have already been shown, the only thing remaining is to show the Doctrine mapping files:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity
name="Product"
table="product">
<id
name="id"
column="id"
type="string"
length="255">
<generator strategy="NONE">
</generator>
</id>

<field
name="name"
type="string"
length="255"
/>

<embedded
name="price"
class="DddDomainModelMoney"
/>
</entity>
</doctrine-mapping>

In the product mapping, we're defining price as an instance variable that will hold a Money instance. At the same time, Money is designed with an amount and a Currency instance:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<embeddable
name="DddDomainModelMoney">

<field
name="amount"
type="integer"
/>
<embedded
name="currency"
class="DddDomainModelCurrency"
/>
</embeddable>
</doctrine-mapping>

Finally, it's time to show the Doctrine mapping for our Currency Value Object:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<embeddable
name="DddDomainModelCurrency">

<field
name="iso"
type="string"
length="3"
/>
</embeddable>
</doctrine-mapping>

As you can see, the above code has a standard embeddable definition with just one string field that holds the ISO code. This approach is the easiest way to use embeddables and is much more effective. By default, Doctrine names your columns by prefixing them using the Value Object name. You can change this behavior to meet your needs by changing the column-prefix attribute in the XML notation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset