1
0
mirror of synced 2024-12-13 14:56:01 +03:00

Merge pull request #669 from hason/many_to_many

Fixed generating column names for self referencing entity.
This commit is contained in:
Benjamin Eberlei 2013-05-25 22:16:48 -07:00
commit 20b5ab26e7
2 changed files with 35 additions and 8 deletions

View File

@ -1551,15 +1551,18 @@ class ClassMetadataInfo implements ClassMetadata
$mapping['joinTable']['name'] = $this->namingStrategy->joinTableName($mapping['sourceEntity'], $mapping['targetEntity'], $mapping['fieldName']); $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'])) { if ( ! isset($mapping['joinTable']['joinColumns'])) {
$mapping['joinTable']['joinColumns'] = array(array( $mapping['joinTable']['joinColumns'] = array(array(
'name' => $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity']), 'name' => $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $selfReferencingEntityWithoutJoinColumns ? 'source' : null),
'referencedColumnName' => $this->namingStrategy->referenceColumnName(), 'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
'onDelete' => 'CASCADE')); 'onDelete' => 'CASCADE'));
} }
if ( ! isset($mapping['joinTable']['inverseJoinColumns'])) { if ( ! isset($mapping['joinTable']['inverseJoinColumns'])) {
$mapping['joinTable']['inverseJoinColumns'] = array(array( $mapping['joinTable']['inverseJoinColumns'] = array(array(
'name' => $this->namingStrategy->joinKeyColumnName($mapping['targetEntity']), 'name' => $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $selfReferencingEntityWithoutJoinColumns ? 'target' : null),
'referencedColumnName' => $this->namingStrategy->referenceColumnName(), 'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
'onDelete' => 'CASCADE')); 'onDelete' => 'CASCADE'));
} }

View File

@ -1042,6 +1042,30 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
$cm->addEntityListener(Events::postLoad, '\Doctrine\Tests\Models\Company\CompanyContractListener', 'invalidMethod'); $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 class MyNamespacedNamingStrategy extends \Doctrine\ORM\Mapping\DefaultNamingStrategy