1
0
mirror of synced 2025-03-05 20:36:15 +03:00

Merge pull request #1561 from deeky666/DDC-4006

[DDC-4006] Inherit ID generator strategy mapping from embeddables
This commit is contained in:
Marco Pivetta 2015-11-19 16:34:32 +01:00
commit 9c5cea3e95
4 changed files with 76 additions and 16 deletions

View File

@ -157,19 +157,7 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
// However this is only true if the hierarchy of parents contains the root entity,
// if it consists of mapped superclasses these don't necessarily include the id field.
if ($parent && $rootEntityFound) {
if ($parent->isIdGeneratorSequence()) {
$class->setSequenceGeneratorDefinition($parent->sequenceGeneratorDefinition);
} else if ($parent->isIdGeneratorTable()) {
$class->tableGeneratorDefinition = $parent->tableGeneratorDefinition;
}
if ($parent->generatorType) {
$class->setIdGeneratorType($parent->generatorType);
}
if ($parent->idGenerator) {
$class->setIdGenerator($parent->idGenerator);
}
$this->inheritIdGeneratorMapping($class, $parent);
} else {
$this->completeIdGeneratorMapping($class);
}
@ -197,6 +185,10 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
$this->addNestedEmbeddedClasses($embeddableMetadata, $class, $property);
}
if (! empty($embeddableMetadata->getIdentifier())) {
$this->inheritIdGeneratorMapping($class, $embeddableMetadata);
}
$class->inlineEmbeddable($property, $embeddableMetadata);
unset($this->embeddablesActiveNesting[$class->name]);
@ -712,6 +704,29 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
}
}
/**
* Inherits the ID generator mapping from a parent class.
*
* @param ClassMetadataInfo $class
* @param ClassMetadataInfo $parent
*/
private function inheritIdGeneratorMapping(ClassMetadataInfo $class, ClassMetadataInfo $parent)
{
if ($parent->isIdGeneratorSequence()) {
$class->setSequenceGeneratorDefinition($parent->sequenceGeneratorDefinition);
} elseif ($parent->isIdGeneratorTable()) {
$class->tableGeneratorDefinition = $parent->tableGeneratorDefinition;
}
if ($parent->generatorType) {
$class->setIdGeneratorType($parent->generatorType);
}
if ($parent->idGenerator) {
$class->setIdGenerator($parent->idGenerator);
}
}
/**
* {@inheritDoc}
*/

View File

@ -0,0 +1,14 @@
<?php
namespace Doctrine\Tests\Models\DDC4006;
/**
* @Entity
*/
class DDC4006User
{
/**
* @Embedded(class="DDC4006UserId")
*/
private $id;
}

View File

@ -0,0 +1,16 @@
<?php
namespace Doctrine\Tests\Models\DDC4006;
/**
* @Embeddable
*/
class DDC4006UserId
{
/**
* @Id
* @GeneratedValue("IDENTITY")
* @Column(type="integer")
*/
private $id;
}

View File

@ -309,7 +309,7 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('parent-id', $user['joinColumns'][0]['name']);
$this->assertEquals('group-id', $user['joinColumns'][0]['referencedColumnName']);
// Address Class Metadata
$this->assertTrue($addressMetadata->fieldMappings['id']['quoted']);
$this->assertTrue($addressMetadata->fieldMappings['zip']['quoted']);
@ -327,11 +327,11 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
// User Class Metadata
$this->assertTrue($userMetadata->fieldMappings['id']['quoted']);
$this->assertTrue($userMetadata->fieldMappings['name']['quoted']);
$this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']);
$this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']);
$address = $userMetadata->associationMappings['address'];
$this->assertTrue($address['joinColumns'][0]['quoted']);
$this->assertEquals('address-id', $address['joinColumns'][0]['name']);
@ -423,6 +423,21 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$cmf->getMetadataFor($metadata->name);
}
/**
* @group DDC-4006
*/
public function testInheritsIdGeneratorMappingFromEmbeddable()
{
$cmf = new ClassMetadataFactory();
$driver = $this->createAnnotationDriver(array(__DIR__ . '/../../Models/DDC4006/'));
$em = $this->_createEntityManager($driver);
$cmf->setEntityManager($em);
$userMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\DDC4006\DDC4006User');
$this->assertTrue($userMetadata->isIdGeneratorIdentity());
}
}
/* Test subject class with overridden factory method for mocking purposes */