Domain-Driven Design, part 7 — Alternative Relational Database Mapping

Product Name Story

Use cases

  • Change the product name in the language.
  • Show the product name in the language
  • If the name in language is missing, show an empty string

LangValue Extraction

Relational Database Storage

Serialization

Example of serialized object

{
"cs-CZ": "Televize",
"en-GB": "Television",
"es-ES": "televisión",
"ru-RU": "телевидение"
}

LangValue Doctrine Mapping

Registration

\Doctrine\DBAL\Types\Type::addType(LangValueType::LANG_VALUE, LangValueType::class);/** @var Doctrine\DBAL\Connection */
$connection = ...;
$platform = $connection->getDatabasePlatform();
$platform->registerDoctrineTypeMapping(LangValueType::LANG_VALUE, LangValueType::LANG_VALUE);

Mapping

Database Search in JSON

PostgreSQL

SELECT * FROM Product where name->>'cs-CZ' LIKE '%query%';

MySQL

SELECT * FROM Product where name->'$.cs-CZ' LIKE '%query%';

TL;DR

References

Contact

--

--

Developer interested in Domain-Driven Design & Modeling

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store