From bef5b585cb30568fc04080058d0e5a523c98ceff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Fri, 17 May 2013 12:08:14 +0200 Subject: [PATCH] Fixed generating join column names for self referencing entity. --- .../ORM/Mapping/ClassMetadataInfo.php | 11 ++++--- .../Tests/ORM/Mapping/ClassMetadataTest.php | 32 ++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index 1e038d659..e4a0955df 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1547,15 +1547,18 @@ class ClassMetadataInfo implements ClassMetadata $mapping['joinTable']['name'] = $this->namingStrategy->joinTableName($mapping['sourceEntity'], $mapping['targetEntity'], $mapping['fieldName']); } + $selfReferencingEntityWithoutJoinColumns = $mapping['sourceEntity'] == $mapping['targetEntity'] + && (! (isset($mapping['joinTable']['joinColumns']) || isset($mapping['joinTable']['inverseJoinColumns']))); + if ( ! isset($mapping['joinTable']['joinColumns'])) { $mapping['joinTable']['joinColumns'] = array(array( - 'name' => $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity']), + 'name' => $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $selfReferencingEntityWithoutJoinColumns ? 'source' : null), 'referencedColumnName' => $this->namingStrategy->referenceColumnName(), 'onDelete' => 'CASCADE')); } if ( ! isset($mapping['joinTable']['inverseJoinColumns'])) { $mapping['joinTable']['inverseJoinColumns'] = array(array( - 'name' => $this->namingStrategy->joinKeyColumnName($mapping['targetEntity']), + 'name' => $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $selfReferencingEntityWithoutJoinColumns ? 'target' : null), 'referencedColumnName' => $this->namingStrategy->referenceColumnName(), 'onDelete' => 'CASCADE')); } @@ -2309,7 +2312,7 @@ class ClassMetadataInfo implements ClassMetadata } $entityResult['entityClass'] = $this->fullyQualifiedClassName($entityResult['entityClass']); - + $resultMapping['entities'][$key]['entityClass'] = ltrim($entityResult['entityClass'], '\\'); $resultMapping['entities'][$key]['isSelfClass'] = $entityResult['isSelfClass']; @@ -2429,7 +2432,7 @@ class ClassMetadataInfo implements ClassMetadata * lifecycle callbacks and lifecycle listeners. * * @deprecated Deprecated since version 2.4 in favor of \Doctrine\ORM\Event\ListenersInvoker - * + * * @param string $lifecycleEvent The lifecycle event. * @param object $entity The Entity on which the event occurred. * diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php index b8e0becc4..8792545ad 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php @@ -340,7 +340,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase 'fieldName' => 'user', 'targetEntity' => 'CmsUser' )); - + $this->assertEquals(array('USER_ID'=>'ID'), $oneToOneMetadata->associationMappings['user']['sourceToTargetKeyColumns']); $this->assertEquals(array('USER_ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['joinColumnFieldNames']); $this->assertEquals(array('ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['targetToSourceKeyColumns']); @@ -348,7 +348,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('USER_ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['name']); $this->assertEquals('ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['referencedColumnName']); - + $this->assertEquals('CMS_ADDRESS_CMS_USER', $manyToManyMetadata->associationMappings['user']['joinTable']['name']); $this->assertEquals(array('CMS_ADDRESS_ID','CMS_USER_ID'), $manyToManyMetadata->associationMappings['user']['joinTableColumns']); @@ -770,7 +770,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase { $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); - + $cm->addSqlResultSetMapping(array( 'name' => 'find-all', 'entities' => array( @@ -1000,7 +1000,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase /** * @group DDC-1955 - * + * * @expectedException Doctrine\ORM\Mapping\MappingException * @expectedExceptionMessage Entity Listener "\InvalidClassName" declared on "Doctrine\Tests\Models\CMS\CmsUser" not found. */ @@ -1025,6 +1025,30 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase $cm->addEntityListener(Events::postLoad, '\Doctrine\Tests\Models\Company\CompanyContractListener', 'invalidMethod'); } + + public function testManyToManySelfReferencingNamingStrategyDefaults() + { + $cm = new ClassMetadata('Doctrine\Tests\Models\CustomType\CustomTypeParent'); + $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); + $cm->mapManyToMany( + array( + 'fieldName' => 'friendsWithMe', + 'targetEntity' => 'CustomTypeParent' + ) + ); + + $this->assertEquals( + array( + 'name' => 'customtypeparent_customtypeparent', + 'joinColumns' => array(array('name' => 'customtypeparent_source', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')), + 'inverseJoinColumns' => array(array('name' => 'customtypeparent_target', 'referencedColumnName' => 'id', 'onDelete' => 'CASCADE')), + ), + $cm->associationMappings['friendsWithMe']['joinTable'] + ); + $this->assertEquals(array('customtypeparent_source', 'customtypeparent_target'), $cm->associationMappings['friendsWithMe']['joinTableColumns']); + $this->assertEquals(array('customtypeparent_source' => 'id'), $cm->associationMappings['friendsWithMe']['relationToSourceKeyColumns']); + $this->assertEquals(array('customtypeparent_target' => 'id'), $cm->associationMappings['friendsWithMe']['relationToTargetKeyColumns']); + } } class MyNamespacedNamingStrategy extends \Doctrine\ORM\Mapping\DefaultNamingStrategy