I am struggling with choosing the right mapping for the following category like scenario :
The food entity has a composed primary key made of 3 fields plus a name field:
╔════════╦═══════╦════════╦════════════════╗
║ family ║ class ║ sector ║ name ║ - family INT UNSIGNED NOT NULL
╠════════╬═══════╬════════╬════════════════╣ - class INT UNSIGNED DEFAULT NULL
║ 1 ║ NULL ║ NULL ║ Natural ║ - sector INT UNSIGNED DEFAULT NULL
║ 1 ║ 2 ║ NULL ║ Greens ║ - name VARCHAR(200) NOT NULL
║ 1 ║ 2 ║ 1 ║ Spring veggies ║ - PRIMARY KEY (family, class, sector)
║ 1 ║ 2 ║ 2 ║ Spring fruits ║
║ 1 ║ 2 ║ 3 ║ Summer veggies ║
╚════════╩═══════╩════════╩════════════════╝
This table is about food categories. Their can be only one entry matching family
+class
+sector
. The more primary key fields are populated, the more "specific" the category record is. A record having family
+class
+sector
(in other words an actual sector category) would have 2 implicit parents: a/ A class record, having same family
and class
but sector
set to NULL, b/ A family record, the top most category, having same family value but both class
and sector
set to NULL.
A sector record would have 0 children but 2 parents meaning $spring_fruit_object->getParents()
would return a collection of food entities like [natural_hydrated_object, greens_hydrated_object]
(eagerly).
Actually, I fear none of the existing associations mapping can handle this usecase automatically given the rules listed above. I will probably have to build custom queries this from the repository class.
How would you handle this scenario? Thank you.