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

Fix root document entityMap #2726

Open
wants to merge 2 commits into
base: 2.10.x
Choose a base branch
from

Conversation

parada85
Copy link
Contributor

@parada85 parada85 commented Feb 4, 2025

Bug Report

Q A
BC Break no
Version 2.9.2

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 :(

Copy link
Member

@GromNaN GromNaN left a 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) {
Copy link
Member

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?

Copy link
Member

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])) {
Copy link
Member

@GromNaN GromNaN Mar 1, 2025

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 for COLLECTION_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.

Copy link
Member

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.

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.

3 participants