1
0
mirror of synced 2025-01-06 00:57:10 +03:00

apply naming strategy on ClassMetadataInfo

This commit is contained in:
Fabio B. Silva 2011-12-23 11:29:50 -02:00
parent 8368f0e4b9
commit 537821418e
3 changed files with 34 additions and 37 deletions

View File

@ -69,18 +69,18 @@ class DefaultNamingStrategy implements NamingStrategy
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function joinTableName($ownerEntity, $associatedEntity, $propertyName = null) public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
{ {
return strtolower($this->classToTableName($ownerEntity) . '_' . return strtolower($this->classToTableName($sourceEntity) . '_' .
$this->classToTableName($associatedEntity)); $this->classToTableName($targetEntity));
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function joinKeyColumnName($propertyEntityName, $referencedColumnName = null, $propertyName = null) public function joinKeyColumnName($entityName, $referencedColumnName = null)
{ {
return strtolower($this->classToTableName($propertyEntityName) . '_' . return strtolower($this->classToTableName($entityName) . '_' .
($referencedColumnName ?: $this->referenceColumnName())); ($referencedColumnName ?: $this->referenceColumnName()));
} }
} }

View File

@ -494,16 +494,25 @@ class ClassMetadataInfo
*/ */
public $isReadOnly = false; public $isReadOnly = false;
/**
* NamingStrategy determining the default column and table names
*
* @var \Doctrine\ORM\NamingStrategy
*/
protected $namingStrategy;
/** /**
* Initializes a new ClassMetadata instance that will hold the object-relational mapping * Initializes a new ClassMetadata instance that will hold the object-relational mapping
* metadata of the class with the given name. * metadata of the class with the given name.
* *
* @param string $entityName The name of the entity class the new instance is used for. * @param string $entityName The name of the entity class the new instance is used for.
* @param NamingStrategy $namingStrategy
*/ */
public function __construct($entityName) public function __construct($entityName, NamingStrategy $namingStrategy = null)
{ {
$this->name = $entityName; $this->name = $entityName;
$this->rootEntityName = $entityName; $this->rootEntityName = $entityName;
$this->namingStrategy = $namingStrategy ?: new \Doctrine\ORM\DefaultNamingStrategy();
} }
/** /**
@ -717,7 +726,7 @@ class ClassMetadataInfo
// Complete fieldName and columnName mapping // Complete fieldName and columnName mapping
if ( ! isset($mapping['columnName'])) { if ( ! isset($mapping['columnName'])) {
$mapping['columnName'] = $mapping['fieldName']; $mapping['columnName'] = $this->namingStrategy->propertyToColumnName($mapping['fieldName']);
} else { } else {
if ($mapping['columnName'][0] == '`') { if ($mapping['columnName'][0] == '`') {
$mapping['columnName'] = trim($mapping['columnName'], '`'); $mapping['columnName'] = trim($mapping['columnName'], '`');
@ -886,8 +895,8 @@ class ClassMetadataInfo
if ( ! isset($mapping['joinColumns']) || ! $mapping['joinColumns']) { if ( ! isset($mapping['joinColumns']) || ! $mapping['joinColumns']) {
// Apply default join column // Apply default join column
$mapping['joinColumns'] = array(array( $mapping['joinColumns'] = array(array(
'name' => $mapping['fieldName'] . '_id', 'name' => $this->namingStrategy->joinColumnName($mapping['fieldName']),
'referencedColumnName' => 'id' 'referencedColumnName' => $this->namingStrategy->referenceColumnName()
)); ));
} }
@ -901,10 +910,10 @@ class ClassMetadataInfo
} }
} }
if (empty($joinColumn['name'])) { if (empty($joinColumn['name'])) {
$joinColumn['name'] = $mapping['fieldName'] . '_id'; $joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName']);
} }
if (empty($joinColumn['referencedColumnName'])) { if (empty($joinColumn['referencedColumnName'])) {
$joinColumn['referencedColumnName'] = 'id'; $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
} }
$mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName']; $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
$mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName']) $mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName'])
@ -965,40 +974,29 @@ class ClassMetadataInfo
{ {
$mapping = $this->_validateAndCompleteAssociationMapping($mapping); $mapping = $this->_validateAndCompleteAssociationMapping($mapping);
if ($mapping['isOwningSide']) { if ($mapping['isOwningSide']) {
if (strpos($mapping['sourceEntity'], '\\') !== false) {
$sourceShortName = strtolower(substr($mapping['sourceEntity'], strrpos($mapping['sourceEntity'], '\\') + 1));
} else {
$sourceShortName = strtolower($mapping['sourceEntity']);
}
if (strpos($mapping['targetEntity'], '\\') !== false) {
$targetShortName = strtolower(substr($mapping['targetEntity'], strrpos($mapping['targetEntity'], '\\') + 1));
} else {
$targetShortName = strtolower($mapping['targetEntity']);
}
// owning side MUST have a join table // owning side MUST have a join table
if ( ! isset($mapping['joinTable']['name'])) { if ( ! isset($mapping['joinTable']['name'])) {
$mapping['joinTable']['name'] = $sourceShortName .'_' . $targetShortName; $mapping['joinTable']['name'] = $this->namingStrategy->joinTableName($mapping['sourceEntity'], $mapping['targetEntity'], $mapping['fieldName']);
} }
if ( ! isset($mapping['joinTable']['joinColumns'])) { if ( ! isset($mapping['joinTable']['joinColumns'])) {
$mapping['joinTable']['joinColumns'] = array(array( $mapping['joinTable']['joinColumns'] = array(array(
'name' => $sourceShortName . '_id', 'name' => $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity']),
'referencedColumnName' => 'id', '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' => $targetShortName . '_id', 'name' => $this->namingStrategy->joinKeyColumnName($mapping['targetEntity']),
'referencedColumnName' => 'id', 'referencedColumnName' => $this->namingStrategy->referenceColumnName(),
'onDelete' => 'CASCADE')); 'onDelete' => 'CASCADE'));
} }
foreach ($mapping['joinTable']['joinColumns'] as &$joinColumn) { foreach ($mapping['joinTable']['joinColumns'] as &$joinColumn) {
if (empty($joinColumn['name'])) { if (empty($joinColumn['name'])) {
$joinColumn['name'] = $sourceShortName . '_id'; $joinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $joinColumn['referencedColumnName']);
} }
if (empty($joinColumn['referencedColumnName'])) { if (empty($joinColumn['referencedColumnName'])) {
$joinColumn['referencedColumnName'] = 'id'; $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
} }
if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') { if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') {
$mapping['isOnDeleteCascade'] = true; $mapping['isOnDeleteCascade'] = true;
@ -1009,10 +1007,10 @@ class ClassMetadataInfo
foreach ($mapping['joinTable']['inverseJoinColumns'] as &$inverseJoinColumn) { foreach ($mapping['joinTable']['inverseJoinColumns'] as &$inverseJoinColumn) {
if (empty($inverseJoinColumn['name'])) { if (empty($inverseJoinColumn['name'])) {
$inverseJoinColumn['name'] = $targetShortName . '_id'; $inverseJoinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $inverseJoinColumn['referencedColumnName']);
} }
if (empty($inverseJoinColumn['referencedColumnName'])) { if (empty($inverseJoinColumn['referencedColumnName'])) {
$inverseJoinColumn['referencedColumnName'] = 'id'; $inverseJoinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
} }
if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') { if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') {
$mapping['isOnDeleteCascade'] = true; $mapping['isOnDeleteCascade'] = true;

View File

@ -65,18 +65,17 @@ interface NamingStrategy
/** /**
* Return a join table name * Return a join table name
* *
* @param string $ownerEntity * @param string $sourceEntity
* @param string $associatedEntity * @param string $targetEntity
* @param string $propertyName * @param string $propertyName
*/ */
function joinTableName($ownerEntity, $associatedEntity, $propertyName = null); function joinTableName($sourceEntity, $targetEntity, $propertyName = null);
/** /**
* Return the foreign key column name for the given parameters * Return the foreign key column name for the given parameters
* *
* @param string $propertyEntityName * @param string $entityName
* @param string $referencedColumnName * @param string $referencedColumnName
* @param string $propertyName
*/ */
function joinKeyColumnName($propertyEntityName, $referencedColumnName = null, $propertyName = null); function joinKeyColumnName($entityName, $referencedColumnName = null);
} }