Skip to content

Instantly share code, notes, and snippets.

@vudaltsov
Last active April 23, 2019 11:31
Show Gist options
  • Save vudaltsov/5bfb22a4f477f2fd714bab6aa9b8246c to your computer and use it in GitHub Desktop.
Save vudaltsov/5bfb22a4f477f2fd714bab6aa9b8246c to your computer and use it in GitHub Desktop.
PHP Money Doctrine XML Mapping
<!-- config/doctrine/money/Currency.orm.xml -->
<?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://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<embeddable name="Money\Currency">
<field name="code" length="3"/>
</embeddable>
</doctrine-mapping>
# config/packages/doctrine.yaml
doctrine:
# ...
orm:
# ...
mappings:
App:
# ...
Money:
is_bundle: false
type: xml
dir: "%kernel.project_dir%/config/doctrine/money"
prefix: Money
alias: Money
<!-- config/doctrine/money/Money.orm.xml -->
<?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://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<embeddable name="Money\Money">
<field name="amount"/>
<embedded name="currency" class="Money\Currency"/>
</embeddable>
</doctrine-mapping>
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Money\Money;
/**
* @ORM\Entity()
*/
class Product
{
// ...
/**
* @ORM\Embedded(Money::class)
*/
private $price;
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment