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'));
} }
@ -2313,7 +2316,7 @@ class ClassMetadataInfo implements ClassMetadata
} }
$entityResult['entityClass'] = $this->fullyQualifiedClassName($entityResult['entityClass']); $entityResult['entityClass'] = $this->fullyQualifiedClassName($entityResult['entityClass']);
$resultMapping['entities'][$key]['entityClass'] = ltrim($entityResult['entityClass'], '\\'); $resultMapping['entities'][$key]['entityClass'] = ltrim($entityResult['entityClass'], '\\');
$resultMapping['entities'][$key]['isSelfClass'] = $entityResult['isSelfClass']; $resultMapping['entities'][$key]['isSelfClass'] = $entityResult['isSelfClass'];
@ -2433,7 +2436,7 @@ class ClassMetadataInfo implements ClassMetadata
* lifecycle callbacks and lifecycle listeners. * lifecycle callbacks and lifecycle listeners.
* *
* @deprecated Deprecated since version 2.4 in favor of \Doctrine\ORM\Event\ListenersInvoker * @deprecated Deprecated since version 2.4 in favor of \Doctrine\ORM\Event\ListenersInvoker
* *
* @param string $lifecycleEvent The lifecycle event. * @param string $lifecycleEvent The lifecycle event.
* @param object $entity The Entity on which the event occurred. * @param object $entity The Entity on which the event occurred.
* *

View File

@ -340,7 +340,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
'fieldName' => 'user', 'fieldName' => 'user',
'targetEntity' => 'CmsUser' 'targetEntity' => 'CmsUser'
)); ));
$this->assertEquals(array('USER_ID'=>'ID'), $oneToOneMetadata->associationMappings['user']['sourceToTargetKeyColumns']); $this->assertEquals(array('USER_ID'=>'ID'), $oneToOneMetadata->associationMappings['user']['sourceToTargetKeyColumns']);
$this->assertEquals(array('USER_ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['joinColumnFieldNames']); $this->assertEquals(array('USER_ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['joinColumnFieldNames']);
$this->assertEquals(array('ID'=>'USER_ID'), $oneToOneMetadata->associationMappings['user']['targetToSourceKeyColumns']); $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('USER_ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['name']);
$this->assertEquals('ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['referencedColumnName']); $this->assertEquals('ID', $oneToOneMetadata->associationMappings['user']['joinColumns'][0]['referencedColumnName']);
$this->assertEquals('CMS_ADDRESS_CMS_USER', $manyToManyMetadata->associationMappings['user']['joinTable']['name']); $this->assertEquals('CMS_ADDRESS_CMS_USER', $manyToManyMetadata->associationMappings['user']['joinTable']['name']);
$this->assertEquals(array('CMS_ADDRESS_ID','CMS_USER_ID'), $manyToManyMetadata->associationMappings['user']['joinTableColumns']); $this->assertEquals(array('CMS_ADDRESS_ID','CMS_USER_ID'), $manyToManyMetadata->associationMappings['user']['joinTableColumns']);
@ -787,7 +787,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
{ {
$cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
$cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); $cm->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService);
$cm->addSqlResultSetMapping(array( $cm->addSqlResultSetMapping(array(
'name' => 'find-all', 'name' => 'find-all',
'entities' => array( 'entities' => array(
@ -1017,7 +1017,7 @@ class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
/** /**
* @group DDC-1955 * @group DDC-1955
* *
* @expectedException Doctrine\ORM\Mapping\MappingException * @expectedException Doctrine\ORM\Mapping\MappingException
* @expectedExceptionMessage Entity Listener "\InvalidClassName" declared on "Doctrine\Tests\Models\CMS\CmsUser" not found. * @expectedExceptionMessage Entity Listener "\InvalidClassName" declared on "Doctrine\Tests\Models\CMS\CmsUser" not found.
*/ */
@ -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