Skip to content

Commit

Permalink
Merge pull request #1561 from deeky666/DDC-4006
Browse files Browse the repository at this point in the history
[DDC-4006] Inherit ID generator strategy mapping from embeddables
  • Loading branch information
Ocramius committed Nov 19, 2015
2 parents 22e76e8 + 86c81da commit 9c5cea3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
41 changes: 28 additions & 13 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,7 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
// 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);
}
Expand Down Expand Up @@ -197,6 +185,10 @@ protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonS
$this->addNestedEmbeddedClasses($embeddableMetadata, $class, $property);
}

if (! empty($embeddableMetadata->getIdentifier())) {
$this->inheritIdGeneratorMapping($class, $embeddableMetadata);
}

$class->inlineEmbeddable($property, $embeddableMetadata);

unset($this->embeddablesActiveNesting[$class->name]);
Expand Down Expand Up @@ -712,6 +704,29 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class)
}
}

/**
* 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}
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/Models/DDC4006/DDC4006User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Doctrine\Tests\Models\DDC4006;

/**
* @Entity
*/
class DDC4006User
{
/**
* @Embedded(class="DDC4006UserId")
*/
private $id;
}
16 changes: 16 additions & 0 deletions tests/Doctrine/Tests/Models/DDC4006/DDC4006UserId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Doctrine\Tests\Models\DDC4006;

/**
* @Embeddable
*/
class DDC4006UserId
{
/**
* @Id
* @GeneratedValue("IDENTITY")
* @Column(type="integer")
*/
private $id;
}
21 changes: 18 additions & 3 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function testQuoteMetadata()
$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']);
Expand All @@ -327,11 +327,11 @@ public function testQuoteMetadata()
// 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']);
Expand Down Expand Up @@ -423,6 +423,21 @@ public function testRejectsEmbeddableWithoutValidClassName()

$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 */
Expand Down

0 comments on commit 9c5cea3

Please sign in to comment.