Support table names different from what was used when generating code

Registered by Johannes Keukelaar on 2009-03-17

Using a function CoughDatabaseFactory::getTableName, cough could support mapping table names to support table names different from what they were when code generation ran. This is useful for hosting sites where only one database is allowed, as this allows multiple instances of the same application to be co-hosted. In my sample application, this function uses a $tableNames array, analogous to getDatabaseName, as well as $tableNamePregPattern and $tableNamePregReplacement to perform regexp replacement.

Blueprint information

Status:
Not started
Approver:
None
Priority:
Undefined
Drafter:
None
Direction:
Needs approval
Assignee:
None
Definition:
Discussion
Series goal:
None
Implementation:
Unknown
Milestone target:
None

Related branches

Sprints

Whiteboard

Sample implementation in CoughGenerator.class:

 public static function getTableName() {
  return CoughDatabaseFactory::getTableName(<?php echo $starterObjectClassName ?>::$tableName);
 }

And in CoughDatabaseFactory:

 /**
  * Get the actual table name for the specified alias.
  *
  * If no mapping exists, it returns the original alias value.
  *
  * @return string
  */
 public static function getTableName($alias)
 {
   if (isset(self::$tableNames[$alias]))
   {
     return self::$tableNames[$alias];
   }
   if (isset(self::$tableNamePregPattern))
   {
     $res = preg_replace(self::$tableNamePregPattern, self::$tableNamePregReplacement, $alias);
     self::$tableNames[$alias] = $res;
     return $res;
   }
   return $alias;
 }

 /**
  * Restore CoughDatabaseFactory to its initial state (no configs, no database
  * objects).
  *
  * @return void
  **/
 public static function reset()
 {
  self::$databases = array();
  self::$databaseNames = array();
  self::$tableNames = array();
  self::$tableNamePregPattern = NULL;
  self::$tableNamePregReplacement = "";
  self::$configs = array();
 }

Snippet from addConfig:

  if (isset($config['table_name_hash']))
  {
    self::$tableNames = $config['table_name_hash'] + self::$tableNames;
  }
  if (isset($config['table_name_pattern']))
  {
    self::$tableNamePregPattern = $config['table_name_pattern'];
  }
  if (isset($config['table_name_replacement']))
  {
    self::$tableNamePregReplacement = $config['table_name_replacement'];
  }

Variable declarations:

 /**
  * Format:
  *
  * [alias] => [actual_table_name]
  *
  * @var array
  */
 protected static $tableNames = array();

 protected static $tableNamePregPattern = NULL;
 protected static $tableNamePregReplacement = "";

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.