mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Lookup/Enum table annotation #6024
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @xxRockOnxx on GitHub (Jul 21, 2018).
Originally assigned to: @Ocramius on GitHub.
Feature Request
Summary
There are times where fetching is really unnecessary. Good example are "Enum tables" or "Lookup table"
Imagine this flow:
Request -> Controller -> Service -> Repo
In your request you might have something like
Where would it fit?
ContactTypeRepo::find()especially when you have deep object.Wouldn't it be a lot easier if we have an entity annotation that look like
You can then set it either the id or the entity itself (without coming from entityManager. Just:
then EntityManager will automatically get the reference for it? Because right now if you do that, the error would be
Workaround
Thoughts?
@Ocramius commented on GitHub (Jul 23, 2018):
No, that won't happen. Doctrine only changes the state of your objects when:
AUTO_INCREMENT)The ORM shouldn't and won't modify any existing fields, and should be limited to persistence and serialisation from/to DB.
For everything that is in-memory, you are directly responsible for using the correct type.
In your case, I think that a lookup table is the incorrect approach, and that a value object in userland is a better alternative.
@xxRockOnxx commented on GitHub (Jul 23, 2018):
Yes I agree. What I am asking is not even related to modification or any operations but rather automatically getting a
Referencefor an entity. As in:instead of (Because in some cases, having
ContactTypeRepois really unncessary)the annotation (what i am asking) would do:
TL;DR: instead of fetching for an entity from entitymanager/repo, just allow setting the Identifier for the entity
I am open for clarifications if still not clear.
Uhm, what? Another scenario for this might
User - Rolerelationship. How can you ensure the integrity if you would only doENUMSin application layer? That's how I understand what you said @Ocramius@Ocramius commented on GitHub (Jul 23, 2018):
Please don't do that in first place: it is overall a bad idea to rely on static global state for this sort of operation
This is still incorrect: it's either an
intor aApp\Models\ContactType. If you want you can use aint|App\Models\ContactTypeinside your DBAL type (what you proposed in the original issue description), but this is clearly a bad practice.Use
EntityManager#getReference()if you are already confident that the value exists, and you rely on foreign key integrity for the check: it converts a given identifier into a proxy or an existing entity of the requested type.One thing is value types, the other is having maps with arbitrary identifiers.
For value types, application-level constraints via value objects are more than sufficient:
Value types are not identifiers to be looked up: they are just values. Don't design a table called
coloursif you know that all possibleColourimplementations are already covered by your value type (that you can also convert to DDL constraints, by the way).For anything that doesn't have upfront known identifiers, you have 3 choices:
EntityManager#find()or equivalentEntityManager#getReference()- I suggest against itEntityManager#find()and a L2 cache configuration (which avoids the DB hit)@oadam commented on GitHub (Nov 2, 2021):
I agree that value types is the right approach.
However, I see only benefits to duplicates your value types in the database + foreign key.
This seems to be the consensus as well on stackoverflow : https://softwareengineering.stackexchange.com/questions/298472/is-it-wasteful-to-create-a-new-database-table-instead-of-using-enum-data-type