couchdb-odm
couchdb-odm copied to clipboard
ID generator strategy not inherited from parent class
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:
- Create a parent class and a child class that inherits from it. Put the ID field in the parent class.
- Define the mapping with an assigned ID strategy.
- 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.
Just updated my original bug report to say that isVersioned and versionField are also not copied from parent to child.