-
-
Notifications
You must be signed in to change notification settings - Fork 928
fix(state): object-mapper reuse related entity #7300
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
fix(state): object-mapper reuse related entity #7300
Conversation
|
||
public function map(object $source, object|string|null $target = null): object | ||
{ | ||
if (isset($this->objectMap[$source])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to check if a target has not been given explicitly by the user before overriding it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my tests the target is a string and I do need to map but I'll test this further.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, if the target is an object it shouldn't be overridden.
I was taking a look at the ObjectMapper, and I have doubts about simple patches in the current implementation - if you add
Would this fix work on this too? [edit] Yes, it should. |
$target = $this->objectMap[$source]; | ||
} | ||
$mapped = $this->decorated->map($source, $target); | ||
$this->objectMap[$mapped] = $source; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you also add $this->objectMap[$source] = $target;
? In persist operations you have multiple back and forth mappings between resource and entity.
This probably should be indexed by the object class too, otherwise multiple mappings will not work - you'll get errors similar to what was fixed by #7311
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a test case for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try to build one, but you can imagine something like a resource that includes 2 different subresources that are actually representations of the same entity. You'd be storing that $entity maps to $firstResource, then on the call for SecondResource::class you'd get back $firstResource.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...and if it was about the first part of my comment, ObjectMapperProcessor does the transformation both ways :
- first the resource is mapped to the entity
- the entity is persisted
- the entity is mapped back to the resource
But as i'm writing this i realize it shouldn't reuse the previous object since its contents can change on persist, so you can just ignore my ramblings :D
|
||
public function map(object $source, object|string|null $target = null): object | ||
{ | ||
if (isset($this->objectMap[$source])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, if the target is an object it shouldn't be overridden.
a9844d7
to
e3a23b7
Compare
e3a23b7
to
024b431
Compare
Fixes an issue where we keep fetched relations so that we don't create new entities instead of persisting the existing one. Check the test and the issue at symfony/symfony#61119.
This needs symfony/symfony#61145 to work.