Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

ID generator strategy not inherited from parent class #87

Open
SteveTalbot opened this issue Feb 20, 2014 · 1 comment
Open

ID generator strategy not inherited from parent class #87

SteveTalbot opened this issue Feb 20, 2014 · 1 comment

Comments

@SteveTalbot
Copy link

Hi,

We encountered difficulties with class inheritance when using an assigned ID strategy. This is because, when field mappings are copied from a parent class to a child, the name of the ID field is copied, but the ID generator strategy is not.

Steps to reproduce:

  1. Create a parent class and a child class that inherits from it. Put the ID field in the parent class.
  2. Define the mapping with an assigned ID strategy.
  3. Instantiate a new child object, assign an ID and try to persist it to the database.

The exception "Detached document passed to persist()" is thrown from line 349 of UnitOfWork. The default ID generator strategy is "UUID", so UnitOfWork incorrectly assumes that the document is detached because the ID field is populated.

We have also discovered that the isVersioned and versionField properties are not copied from parent to child.

Our workaround is to add lines to Doctrine\ODM\CouchDB\Mapping\ClassMetaDataFactory::addFieldMapping() as follows:

    private function addFieldMapping(ClassMetadataInterface $class, ClassMetadataInterface $parent)
    {
        foreach ($parent->reflFields as $name => $field) {
            $class->reflFields[$name] = $field;
        }

        foreach ($parent->fieldMappings as $name => $field) {
            $class->fieldMappings[$name] = $field;
        }

        foreach ($parent->jsonNames as $name => $field) {
            $class->jsonNames[$name] = $field;
        }

        if ($parent->identifier) {
            $class->setIdentifier($parent->identifier);
            $class->idGenerator = $parent->idGenerator;  // <----- added line
        }

        // Added lines from here...
        if ($parent->isVersioned) {
            $class->isVersioned = $parent->isVersioned;
            $class->versionField = $parent->versionField;
        }
        // ... to here
    }

So far this change hasn't broken anything else for us.

@SteveTalbot
Copy link
Author

Just updated my original bug report to say that isVersioned and versionField are also not copied from parent to child.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant