diff --git a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php index 7f92df162..d41553152 100644 --- a/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php +++ b/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php @@ -55,7 +55,17 @@ class DatabaseDriver implements Driver * @var array */ private $manyToManyTables = array(); - + + /** + * @var array + */ + private $classNamesForTables = array(); + + /** + * @var array + */ + private $fieldNamesForColumns = array(); + /** * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading * docblock annotations. @@ -78,7 +88,7 @@ class DatabaseDriver implements Driver { $this->tables = $this->manyToManyTables = $this->classToTableNames = array(); foreach ($entityTables AS $table) { - $className = Inflector::classify(strtolower($table->getName())); + $className = $this->_getClassNameForTable($table->getName()); $this->classToTableNames[$className] = $table->getName(); $this->tables[$table->getName()] = $table; } @@ -120,7 +130,7 @@ class DatabaseDriver implements Driver } else { // lower-casing is necessary because of Oracle Uppercase Tablenames, // assumption is lower-case + underscore separated. - $className = Inflector::classify(strtolower($tableName)); + $className = $this->_getClassNameForTable($tableName); $this->tables[$tableName] = $table; $this->classToTableNames[$className] = $tableName; } @@ -172,7 +182,7 @@ class DatabaseDriver implements Driver continue; } - $fieldMapping['fieldName'] = Inflector::camelize(strtolower($column->getName())); + $fieldMapping['fieldName'] = $this->_getFieldNameForColumn($tableName, $column->getName(), false); $fieldMapping['columnName'] = $column->getName(); $fieldMapping['type'] = strtolower((string) $column->getType()); @@ -226,10 +236,10 @@ class DatabaseDriver implements Driver $localColumn = current($myFk->getColumns()); $associationMapping = array(); - $associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower(current($otherFk->getColumns())))); - $associationMapping['targetEntity'] = Inflector::classify(strtolower($otherFk->getForeignTableName())); + $associationMapping['fieldName'] = $this->_getFieldNameForColumn($manyTable->getName(), current($otherFk->getColumns()), true); + $associationMapping['targetEntity'] = $this->_getClassNameForTable($otherFk->getForeignTableName()); if (current($manyTable->getColumns())->getName() == $localColumn) { - $associationMapping['inversedBy'] = Inflector::camelize(str_replace('_id', '', strtolower(current($myFk->getColumns())))); + $associationMapping['inversedBy'] = $this->_getFieldNameForColumn($manyTable->getName(), current($myFk->getColumns()), true); $associationMapping['joinTable'] = array( 'name' => strtolower($manyTable->getName()), 'joinColumns' => array(), @@ -254,7 +264,7 @@ class DatabaseDriver implements Driver ); } } else { - $associationMapping['mappedBy'] = Inflector::camelize(str_replace('_id', '', strtolower(current($myFk->getColumns())))); + $associationMapping['mappedBy'] = $this->_getFieldNameForColumn($manyTable->getName(), current($myFk->getColumns()), true); } $metadata->mapManyToMany($associationMapping); break; @@ -269,8 +279,8 @@ class DatabaseDriver implements Driver $localColumn = current($cols); $associationMapping = array(); - $associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower($localColumn))); - $associationMapping['targetEntity'] = Inflector::classify($foreignTable); + $associationMapping['fieldName'] = $this->_getFieldNameForColumn($tableName, $localColumn, true); + $associationMapping['targetEntity'] = $this->_getClassNameForTable($foreignTable); for ($i = 0; $i < count($cols); $i++) { $associationMapping['joinColumns'][] = array( @@ -303,4 +313,67 @@ class DatabaseDriver implements Driver return array_keys($this->classToTableNames); } -} \ No newline at end of file + + /** + * Set class name for a table. + * + * @param string $tableName + * @param string $className + * @return void + */ + public function setClassNameForTable($tableName, $className) + { + $this->classNamesForTables[$tableName] = $className; + } + + /** + * Set field name for a column on a specific table. + * + * @param string $tableName + * @param string $columnName + * @param string $fieldName + * @return void + */ + public function setFieldNameForColumn($tableName, $columnName, $fieldName) + { + $this->fieldNamesForColumns[$tableName][$columnName] = $fieldName; + } + + /** + * Return the mapped class name for a table if it exists. Otherwise return "classified" version. + * + * @param string $tableName + * @return string + */ + private function _getClassNameForTable($tableName) + { + if (isset($this->classNamesForTables[$tableName])) { + return $this->classNamesForTables[$tableName]; + } + + return Inflector::classify(strtolower($tableName)); + } + + /** + * Return the mapped field name for a column, if it exists. Otherwise return camelized version. + * + * @param string $tableName + * @param string $columnName + * @param boolean $fk Whether the column is a foreignkey or not. + * @return string + */ + private function _getFieldNameForColumn($tableName, $columnName, $fk = false) + { + if (isset($this->fieldNamesForColumns[$tableName]) && isset($this->fieldNamesForColumns[$tableName][$columnName])) { + return $this->fieldNamesForColumns[$tableName][$columnName]; + } + + $columnName = strtolower($columnName); + + // Replace _id if it is a foreignkey column + if ($fk) { + $columnName = str_replace('_id', '', $columnName); + } + return Inflector::camelize($columnName); + } +}