padam87 / money-bundle
Symfony bundle for https://github.com/moneyphp/money
v0.6.0
2026-03-11 01:48 UTC
Requires
- php: ^8.4
- ext-bcmath: *
- brick/money: ^0.9
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^3.0
- doctrine/orm: ^3.0
- symfony/framework-bundle: ^6.0 || ^7.0 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^7.2
Conflicts
- doctrine/persistence: 3.4.2 || 4.1.1
README
Symfony bundle for https://github.com/brick/money
As an opinionated bundle, this money bundle uses the following principles as it's main guide:
- Storage is just as important as calculation.
- Financial data should be held in SQL, so Doctrine ORM only implementation.
- Money objects are Embeddables.
- Amount should be stored in a human readable way in the database.
- ISO money scale (eg smallest amount is 1 cent for EUR) is not viable for complex applications.
To achieve these, the following restrictions apply:
- precision and scale are mandatory (but have default values)
- amounts are mapped as DECIMAL (changable, but not recommended to change)
- ext-bcmath is mandatory.
Installation
composer require padam87/money-bundle
Configuration (optional)
padam87_money: precision: 18 scale: 2 currencies: # Default: - EUR
Usage
Doctrine
A) Using the embedded money type
#[ORM\Embedded(class: EmbeddedMoney::class)] protected EmbeddedMoney $netPrice; public function getNetPrice(): ?Money { return ($this->netPrice)(); } public function setNetPrice(?Money $netPrice): self { $this->netPrice = new EmbeddedMoney($netPrice); return $this; }
B) Using separate fields for amount and currency
This is recommended when multiple amounts share the same currency
use MoneyFromDecimalTrait; #[ORM\Column(type: 'currency')] private ?Currency $currency = null; #[ORM\Column(type: 'decimal_object')] private ?BigDecimal $netPrice = null; #[ORM\Column(type: 'decimal_object')] private ?BigDecimal $grossPrice = null; public function getCurrency(): ?Currency { return $this->currency; } public function getNetPrice(): Money { return $this->getMoney($this->netPrice); } protected function setNetPrice(Money $netPrice): self { $this->setMoney($this->netPrice, $netPrice); return $this; } public function getGrossPrice(): Money { return $this->getMoney($this->grossPrice); } protected function setGrossPrice(Money $grossPrice): self { $this->setMoney($this->grossPrice, $grossPrice); return $this; }
Formatting
Twig
{{ netPrice|money }} -> €100
{{ netPrice|money_amount }} -> 100
{{ netPrice|money_currency }} -> €