PHPackages                             yokai/doctrine-value-object - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. yokai/doctrine-value-object

ActiveLibrary[Database &amp; ORM](/categories/database)

yokai/doctrine-value-object
===========================

Value Objects for Doctrine ORM simplified

v0.2.0(1y ago)1330.4k↓14.9%11MITPHPPHP ^8.0

Since Feb 14Pushed 1y ago1 watchersCompare

[ Source](https://github.com/yokai-php/doctrine-value-object)[ Packagist](https://packagist.org/packages/yokai/doctrine-value-object)[ RSS](/packages/yokai-doctrine-value-object/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (7)Versions (10)Used By (1)

Value Objects for Doctrine ORM simplified
=========================================

[](#value-objects-for-doctrine-orm-simplified)

[![Tests](https://camo.githubusercontent.com/6d7ab747edf43e211240fc5942278ef9a51cdb053eb06517b2aa787300d3b0f6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f796f6b61692d7068702f646f637472696e652d76616c75652d6f626a6563742f74657374732e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265266c6162656c3d7465737473)](https://github.com/yokai-php/doctrine-value-object/actions)[![Coverage](https://camo.githubusercontent.com/1037171a4d811f3f1d828d362a45c2a38e5ea46e09587b175f20e5205af6ea06/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f796f6b61692d7068702f646f637472696e652d76616c75652d6f626a6563743f7374796c653d666c61742d737175617265)](https://codecov.io/gh/yokai-php/doctrine-value-object)[![Contributors](https://camo.githubusercontent.com/a8a9381ea4a72967bf2115b021af36dddef5eeb1543c70081e81b4c1e77e79c8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f796f6b61692d7068702f646f637472696e652d76616c75652d6f626a6563743f7374796c653d666c61742d737175617265)](https://github.com/yokai-php/doctrine-value-object/graphs/contributors)

[![Latest Stable Version](https://camo.githubusercontent.com/4a31757329705fa2b14a8203048fe0c2ca78a84f842496784db95fe83722ae31/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796f6b61692f646f637472696e652d76616c75652d6f626a6563743f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yokai/doctrine-value-object)[![Downloads Monthly](https://camo.githubusercontent.com/bf0cf2e2f7e65689e371d88609f7c88fd15ced17de162c8c3826463ebd71068a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f796f6b61692f646f637472696e652d76616c75652d6f626a6563743f7374796c653d666c61742d737175617265)](https://packagist.org/packages/yokai/doctrine-value-object/stats)

This library offer you an easy way to handle **value objects** in a **Doctrine ORM** application.

It will save you the creation of Doctrine types for every different value object types you have.

Installation
------------

[](#installation)

```
composer require yokai/doctrine-value-object
```

Setup
-----

[](#setup)

You will have to register your value object types to the Doctrine type system.

```
use Doctrine\DBAL\Types\Type;
use Yokai\DoctrineValueObject\Doctrine\Types;

(new Types(['doctrine_type_name' => MyValueObject::class]))
    ->register(Type::getTypeRegistry());
```

If you are using **Symfony**, you can register your value object types in the kernel construction.

```
use Doctrine\DBAL\Types\Type;
use Yokai\DoctrineValueObject\Doctrine\Types;

class Kernel extends BaseKernel
{
    private const DOCTRINE_VALUE_OBJECTS = [
        'doctrine_type_name' => MyValueObject::class,
    ];

    public function __construct(string $environment, bool $debug)
    {
        parent::__construct($environment, $debug);
        (new Types(self::DOCTRINE_VALUE_OBJECTS))->register(Type::getTypeRegistry());
    }
}
```

You can also leverage the power of attributes, along with [`olvlvl/composer-attribute-collector`](https://github.com/olvlvl/composer-attribute-collector), to automatically register all your value objects at once.

```
use Doctrine\DBAL\Types\Type;
use Yokai\DoctrineValueObject\Doctrine\Types;

#[\Attribute(\Attribute::TARGET_CLASS)]
final readonly class AsValueObject
{
    public function __construct(
        public string $name,
    ) {
    }
}

#[AsValueObject(MyValueObject::DOCTRINE_TYPE_NAME)]
final class MyValueObject implements \Yokai\DoctrineValueObject\StringValueObject
{
    public const DOCTRINE_TYPE_NAME = 'doctrine_type_name';
}

$types = [];
foreach (Attributes::findTargetClasses(AsValueObject::class) as $target) {
    /** @var AsValueObject $attribute */
    $attribute = $target->attribute;
    $types[$attribute->name] = $target->name;
}
(new Types($types))->register(Type::getTypeRegistry());
```

Usage
-----

[](#usage)

After you completed setup you will be able to use your value object types type in any of your entities:

```
