-
-
Notifications
You must be signed in to change notification settings - Fork 509
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 root document entityMap #2726
base: 2.10.x
Are you sure you want to change the base?
Conversation
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.
Hello @parada85, thanks for the exhaustive description and the proposed fix.
I reproduced the error you reported and confirmed that your fix works.
This is also the same approach that is used in the ORM: https://github.com/doctrine/orm/blob/36bef3f959ea9f7f3ea370faf2c803b9be40f5d2/src/UnitOfWork.php#L1753-L1754
Also, could you add some tests to cover this feature?
@@ -2044,7 +2044,7 @@ public function setParentClasses(array $classNames): void | |||
{ | |||
$this->parentClasses = $classNames; | |||
|
|||
if (count($classNames) <= 0) { | |||
if ($this->isEmbeddedDocument || count($classNames) <= 0) { |
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.
Why do you need to add this condition?
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.
Found the response here: #2725 (comment)
@@ -1505,11 +1505,11 @@ public function addToIdentityMap(object $document): bool | |||
$class = $this->dm->getClassMetadata($document::class); | |||
$id = $this->getIdForIdentityMap($document); | |||
|
|||
if (isset($this->identityMap[$class->name][$id])) { | |||
if (isset($this->identityMap[$class->rootDocumentName][$id])) { |
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.
This need to be addressed and covered by tests: #2725 (comment)
I don't remember details about what is stored in
rootDocumentName
but at a glance now there can be a collision forCOLLECTION_PER_CLASS
inheritance type as it's perfectly possible for two separate documents to have same id
We can update the value of ClassMetadata::$rootDocumentName
to contain $name
when the strategy is COLLECTION_PER_CLASS
. That would solve the issue. (Or an new internal property $identityMapName
to keep backward compatibility).
@malarzm any idea?
I don't understand the purpose of COLLECTION_PER_CLASS
, it looks the same as just extending the class as you cannot query the root repository. Having duplicate ids make it even more useless.
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 think I was using COLLECTION_PER_CLASS years ago to work around some mapped superclass limitations, but it's been so long I can't be sure.
Bug Report
Summary
Problem class inheritance save in entitymap
How to reproduce
I have a simple class inheritance like this:
Then I do something like this:
It seems like two database queries are made, but only one should be executed since it should be cached in the entity map. The issue seems to be that when it looks up the entity map, it does so using the repository class, but then stores it with the actual class (the child class) instead.
However, if I do this:
It works correctly, and only one query is made.
When fetching the same document twice, only one database query should be executed if the entity is already cached in the entity map, regardless of whether the parent or child class is used.
To resolve the issue, I have overridden the find method of the DocumentRepository and do the following:
but method tryGetById is marked internal :(