Description
Feature Request
Q | A |
---|---|
New Feature | yes |
RFC | yes |
BC Break | yes/no |
Summary
When relationships are defined the referencedColumnName
is automatically derived from the target entity, unless JoinColumn
is specified. When JoinColumn
is added for any other purpose the referencedColumnName
is hardcoded to id
. I think it should be fetched from the target entity as if no JoinColumn
was defined.
Reasoning
When defining relationships, e.g.:
#[ORM\OneToOne(inversedBy: 'media')]
#[ORM\JoinColumn(name: 'ZATTACHMENT1')]
public Attachment $attachment;
A lot of things are assumed:
- type of the relationship from PHP type
- nullability
- target entity (based on type)
- column name (based on property name transformed by naming strategy via
joinColumnName()
) - reference column name on the other side (based on target entity PK, optionally derived from naming strategy
referenceColumnName()
)
Normally entities also get their primary key column name from the naming strategy, making the JoinColumn
optional. However, as soon as it gets added for a different purpose (like above: to change column name) it creates a strange side effect of referencedColumnName
being overridden by a hardcoded value of id
:
orm/lib/Doctrine/ORM/Mapping/JoinColumn.php
Lines 43 to 45 in 84df37d
Possible improvements
I'm sure there was a reason why this decision was made, but I cannot find it.
The ideal solution, in my opinion, will be defaulting the referencedColumnName
to the target entity primary key, in the same was as when JoinColumn
isn't present on the property. However, this may be seen as a BC break. Changing it now to suddenly read from the naming strategy/target entity may not be the best course of action as applications may rely on this behavior (even if a small number).
However, it's also extremely unintuitive (especially when using PHPDoc-based annotations) to see the referenced column change when the user think adding JoinColumn
is added to only change the column name. The alternative could be explicitly setting the parameter to null
to indicate "figure this out yourself". Currently it's impossible to achieve that - one has to manually specify referencedColumnName
every time JoinColumn
is used.
WDYT? Can this be improved? Currently it leads to a lot of boilerplate code and semi-hardcoded PK name outside of a custom naming strategy.