1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Implemented tableName -> className and columnName -> fieldName mapping in

DatabaseDriver.
This commit is contained in:
Alexander 2011-05-20 16:36:43 +02:00
parent 85fb1a3ebb
commit 262ae7c942

View File

@ -56,6 +56,16 @@ class DatabaseDriver implements Driver
*/ */
private $manyToManyTables = 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 * Initializes a new AnnotationDriver that uses the given AnnotationReader for reading
* docblock annotations. * docblock annotations.
@ -78,7 +88,7 @@ class DatabaseDriver implements Driver
{ {
$this->tables = $this->manyToManyTables = $this->classToTableNames = array(); $this->tables = $this->manyToManyTables = $this->classToTableNames = array();
foreach ($entityTables AS $table) { foreach ($entityTables AS $table) {
$className = Inflector::classify(strtolower($table->getName())); $className = $this->_getClassNameForTable($table->getName());
$this->classToTableNames[$className] = $table->getName(); $this->classToTableNames[$className] = $table->getName();
$this->tables[$table->getName()] = $table; $this->tables[$table->getName()] = $table;
} }
@ -120,7 +130,7 @@ class DatabaseDriver implements Driver
} else { } else {
// lower-casing is necessary because of Oracle Uppercase Tablenames, // lower-casing is necessary because of Oracle Uppercase Tablenames,
// assumption is lower-case + underscore separated. // assumption is lower-case + underscore separated.
$className = Inflector::classify(strtolower($tableName)); $className = $this->_getClassNameForTable($tableName);
$this->tables[$tableName] = $table; $this->tables[$tableName] = $table;
$this->classToTableNames[$className] = $tableName; $this->classToTableNames[$className] = $tableName;
} }
@ -172,7 +182,7 @@ class DatabaseDriver implements Driver
continue; continue;
} }
$fieldMapping['fieldName'] = Inflector::camelize(strtolower($column->getName())); $fieldMapping['fieldName'] = $this->_getFieldNameForColumn($tableName, $column->getName(), false);
$fieldMapping['columnName'] = $column->getName(); $fieldMapping['columnName'] = $column->getName();
$fieldMapping['type'] = strtolower((string) $column->getType()); $fieldMapping['type'] = strtolower((string) $column->getType());
@ -226,10 +236,10 @@ class DatabaseDriver implements Driver
$localColumn = current($myFk->getColumns()); $localColumn = current($myFk->getColumns());
$associationMapping = array(); $associationMapping = array();
$associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower(current($otherFk->getColumns())))); $associationMapping['fieldName'] = $this->_getFieldNameForColumn($manyTable->getName(), current($otherFk->getColumns()), true);
$associationMapping['targetEntity'] = Inflector::classify(strtolower($otherFk->getForeignTableName())); $associationMapping['targetEntity'] = $this->_getClassNameForTable($otherFk->getForeignTableName());
if (current($manyTable->getColumns())->getName() == $localColumn) { 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( $associationMapping['joinTable'] = array(
'name' => strtolower($manyTable->getName()), 'name' => strtolower($manyTable->getName()),
'joinColumns' => array(), 'joinColumns' => array(),
@ -254,7 +264,7 @@ class DatabaseDriver implements Driver
); );
} }
} else { } 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); $metadata->mapManyToMany($associationMapping);
break; break;
@ -269,8 +279,8 @@ class DatabaseDriver implements Driver
$localColumn = current($cols); $localColumn = current($cols);
$associationMapping = array(); $associationMapping = array();
$associationMapping['fieldName'] = Inflector::camelize(str_replace('_id', '', strtolower($localColumn))); $associationMapping['fieldName'] = $this->_getFieldNameForColumn($tableName, $localColumn, true);
$associationMapping['targetEntity'] = Inflector::classify($foreignTable); $associationMapping['targetEntity'] = $this->_getClassNameForTable($foreignTable);
for ($i = 0; $i < count($cols); $i++) { for ($i = 0; $i < count($cols); $i++) {
$associationMapping['joinColumns'][] = array( $associationMapping['joinColumns'][] = array(
@ -303,4 +313,67 @@ class DatabaseDriver implements Driver
return array_keys($this->classToTableNames); return array_keys($this->classToTableNames);
} }
/**
* 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);
}
} }