Skip to content
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

Root document Entity Map #2725

Closed

Conversation

parada85
Copy link
Contributor

@parada85 parada85 commented Feb 4, 2025

Bug Report

Q A
BC Break no
Version 2.10.x

Summary

Problem class inheritance save in entitymap

How to reproduce

I have a simple class inheritance like this:

#[ODM\DiscriminatorMap([3 => Tag::class, 4 => Category::class])]
class Section {}


class Tag extends Section {}
class Category extends Section {}

Then I do something like this:

/** @var DocumentManager $manager */
$manager = Kernel::stGetObj()->getContainer()->get('doctrine_mongodb')->getManager();
$manager->getRepository(Section::class)->find(68);
$manager->getRepository(Section::class)->find(68);

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.

18:33:35 DEBUG     [doctrine] MongoDB command: {"find":"section","filter":{"_id":68,"type":{"$in":[3,4]}},"limit":1,"$db":"db","lsid":{"id":{"$binary":"R5St6qBWSAqg\/2jT16lktA==","$type":"04"}}}
18:33:35 DEBUG     [doctrine] MongoDB command: {"find":"section","filter":{"_id":68,"type":{"$in":[3,4]}},"limit":1,"$db":"db","lsid":{"id":{"$binary":"R5St6qBWSAqg\/2jT16lktA==","$type":"04"}}}

However, if I do this:

/** @var DocumentManager $manager */
$manager = Kernel::stGetObj()->getContainer()->get('doctrine_mongodb')->getManager();
$manager->getRepository(Tag::class)->find(68);
$manager->getRepository(Tag::class)->find(68);

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:

$parentClass = $this->getReflectionParentClass(new \ReflectionClass($documentName));        
if ($map = $this->getDocumentManager()->getClassMetadata($parentClass->getName())->discriminatorMap) {
     foreach ($map as $class) {
        if ($ret = $this->getDocumentManager()->getUnitOfWork()->tryGetById($id, $this->getDocumentManager()->getClassMetadata($class))) {
            return $ret;
          }
     }
}

but method tryGetById is marked internal :(

@parada85
Copy link
Contributor Author

parada85 commented Feb 4, 2025

I am having issues with embedded documents because I don't know how the entity map should behave with these types of documents.

return false;
}

$this->identityMap[$class->name][$id] = $document;
$this->identityMap[$class->rootDocumentName][$id] = $document;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember details about what is stored in rootDocumentName but at a glance now there can be a collision for COLLECTION_PER_CLASS inheritance type as it's perfectly possible for two separate documents to have same id

@malarzm
Copy link
Member

malarzm commented Feb 4, 2025

I am having issues with embedded documents because I don't know how the entity map should behave with these types of documents.

Embedded documents are not reusable in any way, no can exist on their own. If they weren't included in the identity map, they still shouldn't be. If they were, we were most likely using spl_object_hash/id and the change should be pretty transparent

@parada85
Copy link
Contributor Author

parada85 commented Feb 4, 2025

I am having issues with embedded documents because I don't know how the entity map should behave with these types of documents.

Embedded documents are not reusable in any way, no can exist on their own. If they weren't included in the identity map, they still shouldn't be. If they were, we were most likely using spl_object_hash/id and the change should be pretty transparent

@parada85 parada85 closed this Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants