docs: Unwrapping Nested Objects using Embeddables or Custom Types #5521
Replies: 1 comment 2 replies
-
I will get back to this later, for now just a few quick comments.
Instead of asking "show me how to do this", please provide a complete but minimal reproduction so I can actually start experimenting right ahead with the problems you see - that's the fastest way to find an actual resolution. You should use embeddables for this. I would stay away from rather internal options like
This is a non-goal to me, custom types abstract a single-column transform only and I don't plan to change this in the foreseeable future.
The only best practice I can offer is this: keep things simple, what you are up to sounds like a lot of abstraction already, but I guess that's not what you wanted to hear :] |
Beta Was this translation helpful? Give feedback.
-
Context
In a domain-driven design project utilizing MikroORM, I use value objects like
Period
that encapsulate properties within a_value
structure. My goal is to persist thestart
andend
properties as separate columns (period_start
andperiod_end
) in the database, instead of storing them as a JSON object. However, I face challenges with hydrating and persisting these properties as individual columns.Value Object Definition
The
Period
value object is defined as follows, encapsulating its properties and exposing them through getters:The base
ValueObject
class is as follows:Embeddable Schema Configuration
Setting up the entity schema presented challenges, particularly because the properties are not directly accessible due to their encapsulation within a
_value
object. Attempting to configure hydration settings did not resolve these issues:An error occurs stating,
Cannot set property start of #<Period> which has only a getter
, even when hydration is disabled.Desired Database Format Conversion
I aim to convert entities with a structure like this:
Into a format like this in the database:
And then be able to restore the original entity, when reading from the database.
While I successfully converted the
id
using a custom type, theperiod
, being an object, posed difficulties.I tried to make it work through the following schema:
And the
LectureIdSchemaType
is:Questions and Requests for Guidance
I would greatly appreciate any guidance or recommendations on how to resolve these issues.
Beta Was this translation helpful? Give feedback.
All reactions