Class CoughDatabaseFactory

Description

A simple factory that provides access to an application's database objects.

It should be dynamically initialized, and can hold mixed types of database adapter objects.

EXAMPLE RETRIEVAL:

  1.      $db CoughDatabaseFactory::getDatabase('alias1');
  2.      $dbName CoughDatabaseFactory::getDatabaseName('alias1');

EXAMPLE INITIALIZATION:

Assuming we have:

  1.      $configs array(
  2.          array(
  3.              'db_name_hash' => array('alias1' => 'actual_db_name'),
  4.              'adapter' => 'as',
  5.              'driver' => 'mysql',
  6.              'host' => 'localhost',
  7.              'user' => 'nobody',
  8.              'pass' => '',
  9.              'port' => 3307,
  10.          ),
  11.          array(
  12.              'db_name_hash' => array('my_app' => 'my_app'),
  13.              'adapter' => 'as',
  14.              'driver' => 'mysql',
  15.              'host' => 'localhost',
  16.              'user' => 'nobody',
  17.              'pass' => '',
  18.              'port' => 3306,
  19.          )
  20.      );

Option 1: Add one config at a time:

  1.      CoughDatabaseFactory::addConfig($configs[0]);
  2.      CoughDatabaseFactory::addConfig($configs[1]);

Option 2: Add all configs at once:

  1.      CoughDatabaseFactory::setConfigs($configs);

The config array supports other parameters as well, but you shouldn't have to use them. See $configs for all available options.

Located in /cough/CoughDatabaseFactory.class.php (line 62)


	
			
Variable Summary
static array $configs
static array $databaseNames
static array $databases
Method Summary
static void addConfig ( $config)
static void addDatabase ( $alias,  $dbObject)
static array getConfigs ()
static CoughDatabaseInterface|null getDatabase (string $alias, [string $dbName = null])
static string getDatabaseName ( $alias)
static array getDatabases ()
static array getUniqueDatabases ()
static void reset ()
static void setConfigs ( $configs)
Variables
static array $configs = array() (line 129)

An array of database config info.

When a database is retrieved, if it is not already created, then it gets created using the configuration info specified in this array, if that info exists.

Format:

  1.      array(
  2.          # Which database adapter (DAL) to use. optional, default: as
  3.          'adapter' => 'as',
  4.  
  5.          # Adapter class name prefix. optional, default: 'Cough' plus the titlecase of adapter
  6.          'adapter_class_prefix' => 'CoughAs',
  7.  
  8.          # Adapter location. optional, default: cough module directory plus "dal" plus adapter
  9.          'adapter_class_path' => '/my/path/',
  10.  
  11.          # Hash of aliases to actual database names.  Most likely the alias
  12.          # will be the name of the database that generation took place on and the
  13.          # actual database name will be the same.  If a different environment
  14.          # (e.g. production/test/dev) uses a different database name, then just
  15.          # change the actual database name to that database in that environment's
  16.          # config file, leaving the alias/key part of the hash alone.
  17.          # required if old "aliases" param not given.
  18.          'db_name_hash' => array(
  19.              'alias1' => 'actual_db_name1',
  20.              'alias2' => 'actual_db_name2'
  21.          ),
  22.  
  23.          # old way of specifying connection aliases did not include db name
  24.          # remapping ability. Use "db_name_hash" instead.
  25.          'aliases' => array('actual_db_name1''actual_db_name2'),
  26.  
  27.          # the rest of these should be obvious
  28.          'driver' => 'mysql',
  29.          'host' => 'localhost',
  30.          'user' => 'nobody',
  31.          'pass' => '',
  32.          'port' => 3306
  33.      );

  • access: protected
static array $databaseNames = array() (line 80)

Format:

[alias] => [actual_db_name]

  • access: protected
static array $databases = array() (line 71)

Format:

[alias] => [CoughDatabaseInterface]

  • access: protected
Methods
static method addConfig (line 154)

Adds the database config for later use. Make sure to specified the 'aliases' or the 'db_name_hash' value.

static void addConfig ( $config)
  • $config
static method addDatabase (line 194)

Adds the database object for the specified alias name.

It's better to add configs b/c then a database object/connection won't be made unless one is needed.

static void addDatabase ( $alias,  $dbObject)
  • $alias
  • $dbObject
static method constructDatabaseByConfig (line 267)

returns the correct database adapter object

static CoughDatabaseInterface constructDatabaseByConfig ( $dbConfig)
  • $dbConfig
static method getConfigs (line 303)

Get all the database configs CoughDatabaseFactory is currently aware of.

Could be useful for debugging purposes.

static array getConfigs ()
static method getDatabase (line 206)

Get the database object for the specified alias

  • access: public
static CoughDatabaseInterface|null getDatabase (string $alias, [string $dbName = null])
  • string $alias
  • string $dbName: optional database name to select before returning the database object.
static method getDatabaseName (line 251)

Get the actual database name for the specified alias.

If no mapping exists, it returns the original alias value.

  • access: public
static string getDatabaseName ( $alias)
  • $alias
static method getDatabases (line 315)

Get all the currently constructed database objects.

Could be useful for debugging purposes.

static array getDatabases ()
static method getUniqueDatabases (line 341)

Same as getDatabases(), except it rolls up all aliases using the same connection into one array entry.

  • return: of objects that implement CoughDatabaseInterface
  • author: Anthony Bush
  • since: 2008-09-09
  • access: public
static array getUniqueDatabases ()
static method reset (line 326)

Restore CoughDatabaseFactory to its initial state (no configs, no database objects).

  • access: public
static void reset ()
static method setConfigs (line 138)

Sets all the database configs at once.

static void setConfigs ( $configs)
  • $configs

Documentation generated on Tue, 23 Sep 2008 22:48:22 -0500 by phpDocumentor 1.4.0