mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
ManyToOne inversedBy an Entity with a Derived Identity : Cannot assign int to App\Entity\B::$a of type ?App\Entity\A
#7091
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 @DaedalusDev on GitHub (Jan 13, 2023).
Bug Report
2.14.02.13.5Summary
In multiple cases in an application, we use
Entityas identifier to make it easier, in particular for cases of OneToOne relationships Identity through foreign Entities. Unfortunately, we are facing an issue when we try to create a child association mapping for the child entities.It works as expected in parent to child fetching. But it failed at child to parent fetching.
Current behavior
Fetching child association field produce an error in hydratation process.
How to reproduce
Error
testFetchCfail with the following error :Expected behavior
Doctrine didn't analyse
B::$amapping, and tried to allocate an integer instead ofAtoB::$a(instance ofAorAProxyclass).This is a very very particular case, but i didn't find an option to cast
ORM\JoinColumnto another type than*ToOne::targetEntity.Just tell me if this is an interesting feature for a future version of Doctrine or if I missed something in the doc 🤐
Related issue
I've found this issue #3093
It seems to relate to a similar problem even if the schema is not exactly the same. Unfortunately, it was classified as "Can't Fix"
Workaround
The most simple workaround are :
@ORM\GeneratedValueonBCdirectly toAwith@ORM\ManyToOne(targetEntity=A::class, inversedBy="cs")Solution proposal
Solution 1 (declarative)
Add an optionnal extra argument
targetIdentifierEntityattribute forManyToOne.Solution 2 (automatic)
Ensure identifier is
integerbeforesetValuecall of the entityReflexionClass(Can come with performance consideration ?)Just tell me if this is an interesting feature for a future version of Doctrine or if I missed something in the doc 🤐
Thanks for your great work !
@mpdude commented on GitHub (Jan 16, 2023):
You're using
B::$aas the@Idcolumn and as an@OneToOnerelationship at the same time?I cannot see how this is supposed to work. To give just one reason, the
$acolumn would have to contain an object and the identifier value at the same time?@DaedalusDev commented on GitHub (Jan 17, 2023):
@mpdude Yes, it works very well and we have several entities that use this mode of operation in order to compartmentalize the data. Concerning
B::$a, this value doesn't contain the identifier value but an instance ofAor a proxy class ofAexactly as expected. Current version of doctrine/ORM manage perfecly this use case.To functionally illustrate the use case, here is a substitution example :
For an
Agency(Ain previous exemple) entity that represents a customer, we have the possibility to define anAgencySettingsSubscription(Bin previous exemple) entity which contains the billing settings of theAgencyentity.For the
AgencySettingsSubscriptionentity, generating an identifier is superflux and not necessary because all agencies have unique billing settings. So theidis directly theidof the Agency entity.Now we want to add several
PaymentMethod(Cin previous exemple) entities that contain the agency's payment information. However we want to attach this information to theAgencySettingsSubscriptionentity rather than theAgencyentity.Here is a JSON that represents the expected data format :
@mpdude commented on GitHub (Jan 17, 2023):
I am not in a position to judge, but it seems to me you're overstressing what the ORM was built to do.
I'd say the
@Idmay be of scalar types and possibly a composite key, but definetly not a column that is a relation at the same time.Let's wait what others have to say. Possibly a mapping check (Schema Tool? Runtime validation?) should reject this kind of configuration.
@DaedalusDev commented on GitHub (Jan 17, 2023):
See Identity through foreign Entities for more details about using an entity as identifier (primary or composite) 👍
I've update bug's description and initial issue to be clearer about our intentions 😉
@DaedalusDev commented on GitHub (Jan 17, 2023):
Ok... My bad ! My issue runs on 2.13.5 and is fixed in 2.14.x !
Closed !
@toby-griffiths commented on GitHub (Sep 7, 2023):
@DaedalusDev Thank you so much for you sharing that fix. We've ust spent a couple of hours trying to get to the bottom of this exact same issue.