<commit_before>```json
{
  "layout": "post",
  "title": "How to persist Value Object with Doctrine ODM",
  "date": "2019-06-09 10:00:00",
  "categories": ["value object", "value type", "doctrine odm"],
  "comments": true
}
```
----------
I have been working the last couple years with Doctrine ODM in production environment and in some situations I needed to improve my code applying the concept of Value Object/Type, that is a very important pattern to avoid creation of invalid object and to improve the design of the domain code.

In this post I will describe a short review of Value Object/Type and then show how to apply this concept and persist on the database using Doctrine ODM.

## Short review about Value Object/Type

In some books you can see the author calling this pattern of Value Object and in other of Value Type, in this post from now on I will call it just of Value Object.

Value Objects are small objects that represents a value, two Value Objects with the same state are equals, the main characteristics of a Value Object are that it is immutable and does not have an identity. We can use Value Object to help create a consistent domain model that could be easier to understand, such as, if we create a `Money` Value Object in our code instead of just using `float`, we can let all the rules that is relevant to create a money value inside the same object, then if we need to change some of these rules we just go in one place, also all the objects that need a money type will have it already validated, these things can reduce the risk of confusion and duplication.

We can find more about this concept of Value Object in many books or articles, like any books of DDD by <NAME> by <NAME>, Refactoring by <NAME>, Growing Objects-Oriented Software by <NAME> and <NAME>, and a many other books or articles that have as the goal improve the software design and consistency.

So, before I start showing the code, let's assume that our Value Object definition is, like described in [Martin Fowler's article](https://martinfowler.com/bliki/ValueObject.html):

> *"Objects that are equal due to the value of their properties..."*

## Persisting Value Object with Doctrine ODM

To persist Value Object using Doctrine ODM I can simply use the [Custom Mapping Types](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/reference/basic-mapping.html#custom-mapping-types) or [@EmbedOne Annotation](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/reference/annotations-reference.html#embedone) that the framework provides. Beyond Doctrine ODM I will also install other two packages, one to create Uuid, that is [ramsey/uuid](https://github.com/ramsey/uuid) and other one to help validate the input data, that is [beberlei/assert](https://github.com/beberlei/assert). <commit_msg>fix english post about value object<commit_after>```json
{
  "layout": "post",
  "title": "How to persist Value Object with Doctrine ODM",
  "date": "2019-06-09 10:00:00",
  "categories": ["value object", "value type", "doctrine odm"],
  "comments": true
}
```
----------
I have been working the last couple years with Doctrine ODM in production environment and in some situations I needed to improve my code applying the concept of Value Object/Type, that is a very important pattern to avoid creation of invalid object and to improve the design of the domain code.

In this post I will describe a short review of Value Object/Type and then show how to apply this concept and persist on the database using Doctrine ODM.

## Short review about Value Object/Type

In some books you can see the author calling this pattern of Value Object and in other of Value Type, in this post from now on I will call it just of Value Object.

Value Objects are small objects that represents a value, two Value Objects with the same state are equals, the main characteristics of a Value Object are that it is immutable and does not have an identity. We can use Value Object to help create a consistent domain model that could be easier to understand, such as, if we create a `Money` Value Object in our code instead of just using `float`, we can let all the rules that is relevant to create a money value inside the same object, then if we need to change some of these rules we just go in one place, also all the objects that need a money type will have it already validated, these things can reduce the risk of confusion and duplication.

We can find more about this concept of Value Object in many books or articles, like any books of DDD by <NAME> by <NAME>, Refactoring by <NAME>, Growing Objects-Oriented Software by <NAME> and <NAME>, and a many other books or articles that have as the goal improve the software design and consistency.

So, before I start showing the code, let's assume that our Value Object definition is, as described in [Martin Fowler's article](https://martinfowler.com/bliki/ValueObject.html):

> *"Objects that are equal due to the value of their properties..."*

## Persisting Value Object with Doctrine ODM

To persist Value Object using Doctrine ODM I can simply use the [Custom Mapping Types](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/reference/basic-mapping.html#custom-mapping-types) or [@EmbedOne Annotation](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/1.2/reference/annotations-reference.html#embedone) that the framework provides. Beyond Doctrine ODM I will also install other two packages, one to create Uuid, that is [ramsey/uuid](https://github.com/ramsey/uuid) and other one to help validate the input data, that is [beberlei/assert](https://github.com/beberlei/assert). 
{'fraction_non_alphanumeric': 0.0708245243128964, 'fraction_numerical': 0.006342494714587738, 'mean_word_length': 4.4586538461538465, 'pattern_counts': {'":': 10, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 10, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '676599', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1416, 'n_words': 829}