Merge pull request #669 from hason/many_to_many
Fixed generating column names for self referencing entity.
This commit is contained in:
commit
20b5ab26e7
@ -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'));
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user