-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Violation of Doctrine Rule about foreign keys #16
Comments
@borNfreee I think It could be because the sample uses two approaches: 1.- User is the aggregate root for Wishes. In the second case you need that UserId to kmow who is the User who has that wish. But why do not we store the complete User reference inside the Wish instead of only the UserId? If something I do not know. I asked something similar some time ago in the DDDinPHP google group: and @carlosbuenosvinos replied me this:
|
There's no rule like that in the ORM: it's a choice. |
@Ocramius If we are mapping foreign key to the object property, should we also define a relation? |
Does it need to be a FK? Is eventual consistency acceptable? |
@Ocramius What do you mean by Eventual Consistency here?
This question above is not related to ES/CQRS btw. Let's have a simple example. Doing all this stuff in Doctrine, I don't understand how to achieve this - how to have So the question is simple: does Doctrine support a mapped object property to be a FK? From what I have read in SO, Hibernate does support it. |
This. This is usually acceptable when building references throughout BCs or separate aggregates.
You just map the field as an integer, or as a
That's exactly what is usually denied when referencing across aggregates. It should be treated as a separate, different-address-space DB. |
Really cool discussion here guys, thanks a lot for your contributions. I think @Ocramius has answered pretty well your question. The Domain Model shouldn't be aware of the mechanics of the underlying persistence mechanism, it does not matter if is Doctrine or raw SQL. The UserId defines the identity of the User related to a specific Wish at the Domain level, it has nothing to do with the persistence mechanism. You might persist it over an In-Memory DB, Mongo or even a file where you don't have the concept of a Foreign Key. You need that so your SQL queries are optimal. Remember that he Domain does not care about low-level implementation details. At the Infrastructure level, you can map the UserId Value Object to a SQL Foreign Key easily with Doctrine custom types. Check out this example. I hope we've shed some light on the subject. Closing the issue now :) |
Thank you all for descriptive and useful inputs! |
Hi,
you are mapping foreign keys to object properties which is forbidden from Doctrine point of view.
Could you comment it? How to deal with it without violation this rule?
Why do you need
userId
insideWish
entity if you have one-to-many through join table?http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/best-practices.html#don-t-map-foreign-keys-to-fields-in-an-entity
The text was updated successfully, but these errors were encountered: