Skip to content

Commit

Permalink
Merge pull request #669 from hason/many_to_many
Browse files Browse the repository at this point in the history
Fixed generating column names for self referencing entity.
  • Loading branch information
beberlei committed May 26, 2013
2 parents a36f84e + bef5b58 commit 20b5ab2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
11 changes: 7 additions & 4 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Expand Up @@ -1551,15 +1551,18 @@ protected function _validateAndCompleteManyToManyMapping(array $mapping)
$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'));
}
Expand Down Expand Up @@ -2313,7 +2316,7 @@ public function addSqlResultSetMapping(array $resultMapping)
}

$entityResult['entityClass'] = $this->fullyQualifiedClassName($entityResult['entityClass']);

$resultMapping['entities'][$key]['entityClass'] = ltrim($entityResult['entityClass'], '\\');
$resultMapping['entities'][$key]['isSelfClass'] = $entityResult['isSelfClass'];

Expand Down Expand Up @@ -2433,7 +2436,7 @@ public function setCustomRepositoryClass($repositoryClassName)
* 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.
*
Expand Down
32 changes: 28 additions & 4 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Expand Up @@ -340,15 +340,15 @@ public function testUnderscoreNamingStrategyDefaults()
'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']);

$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']);
Expand Down Expand Up @@ -787,7 +787,7 @@ public function testNamingCollisionSqlResultSetMappingShouldThrowException()
{
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);

$cm->addSqlResultSetMapping(array(
'name' => 'find-all',
'entities' => array(
Expand Down Expand Up @@ -1017,7 +1017,7 @@ public function testInvalidOverrideAttributeFieldTypeException()

/**
* @group DDC-1955
*
*
* @expectedException Doctrine\ORM\Mapping\MappingException
* @expectedExceptionMessage Entity Listener "\InvalidClassName" declared on "Doctrine\Tests\Models\CMS\CmsUser" not found.
*/
Expand All @@ -1042,6 +1042,30 @@ public function testInvalidEntityListenerMethodException()

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

0 comments on commit 20b5ab2

Please sign in to comment.