mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Associations Being loaded with the wrong type and wrong Id #5282
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 @warslett on GitHub (Oct 3, 2016).
I have an entity that has two ManyToOne associations to two different classes of entity. When the entity is loaded from the repo, both associations are loaded with the same class (so one association has the correct class and the other has the wrong class). The other strange behaviour is that the IDs have been switched around so the id for the first association is loaded for the second association and the id of the second association is loaded for the first association.
These issues only seem to effect retrieving the entity, creating the entity seems to have no problems and the data this is mapped to was created using the same entities with the same association mappings. The data that gets set in the database when these entities are persisted is all correct, the correct Ids are being set on the correct columns. The only thing I think I'm doing that is unusual in any way is I am using a legacy database schema so the schema is not generated by doctrine and there are some columns on the table that are not mapped with doctrine.
My class and mappings without the methods:
Database:
@warslett commented on GitHub (Oct 3, 2016):
An update on this. I seem to get the correct data when I use a repository method to get the offending entities. The problem only seems to occur when I get it from an association:
Then when getAcademyTasks is run I get these weird broken entities back
@lcobucci commented on GitHub (Oct 3, 2016):
@warslett that sounds weird... it looks like a mapping issue but I'm not 100% sure. Could you try to reproduce this behavior on a functional test? You can use these tests as reference.
You don't need necessarily to fix the issue but sending a PR with the tests would be really helpful.
@warslett commented on GitHub (Oct 3, 2016):
@lcobucci I'll try and get a pull request sent in the next few days
@lcobucci commented on GitHub (Oct 3, 2016):
Cool, thanks! Ping me if need any help to create the test
@warslett commented on GitHub (Nov 1, 2016):
Sorry, solved this ages ago but never closed this issue. The problem was due to user error I'm afraid. The academy entity was mapped incorrectly
Note mappedBy="task" should have been mappedBy="academy". The issue wasn't obvious because it was loading the AcademyTask association, just with the wrong data which is why we were expecting the issue to be with the mappings on the AcademyTask entity and after checking them several times concluded that there was nothing wrong with our code.
I suppose it would be more useful if Doctrine could validate this kind of thing and throw a useful error. the association on the Academy mapped by task can never be valid because the task property is not of type Academy.