Doctrine2 ORM and NoSQL

In the SymfonyCat, @beberlei, presents an awesome talk about how to deal with few use cases using doctrine, like EAV, see: Doctrine ORM and NoSQL.

MySQL introduces json data type in 5.7.8 witch its similar to a primitive NoSQL.

Lets see how it works in the real life:


Having a Wallet with real money and bonus money, and transactions to track the changes on this wallets.
This transactions have some transaction types:

To store all in the same table I use the doctrine inheritance with a single table strategy, really easy to implement.

Each transaction has his own custom details and this can be stored as json_array in doctrine but at the end you'll have an array soup. Instead of that, we'll use the json data type and create our custom data types. Think simple: An object that serializes into json and the inverted direction. We'll say doctrine how to convert this json into the object we want and when. So, lets go:

Create a Custom type

Create a new custom type its easy, see here. Create a new JsonDocumentType.
Now we'll use this Type to convert DepositDetails into a doctrine custom data type, its a symfony application so add the doctrine mapping into Bundle boot method, by that way you can easily inject the serializer via contianer.
And last step its tell doctrine when to use it, on Deposit doctrine mapping.

Repeated the process with the rest of the transaction types, now its time see the results, for example, a rollback withdrawal scenario.

Database result

To use the MySQL JSON functionsin DQL I found:

So we did it and its works, so just say thanks to beberlei for his great talk.