DB_Table
[ class tree: DB_Table ] [ index: DB_Table ] [ all elements ]

Class: DB_Table_Generator

Source Location: /DB_Table-1.5.6/DB/Table/Generator.php

Class Overview


class DB_Table_Generator - Generates DB_Table subclass skeleton code


Author(s):

Version:

  • Release: 1.5.6

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 198]
class DB_Table_Generator - Generates DB_Table subclass skeleton code

This class generates the php code necessary to use the DB_Table package to interact with an existing database. This requires the generation of a skeleton subclass definition be generated for each table in the database, in which the $col, $idx, and $auto_inc_col properties are constructed using a table schema that is obtained by querying the database.

The class can also generate a file, named 'Database.php' by default, that includes (require_once) each of the table subclass definitions, instantiates one object of each DB_Table subclass (i.e., one object for each table), instantiates a parent DB_Table_Database object, adds all the tables to that parent, attempts to guess foreign key relationships between tables based on the column names, and adds the inferred references to the parent object.

All of the code is written to a directory whose path is given by the property $class_write_path. By default, this is the current directory. By default, the name of the class constructed for a table named 'thing' is "Thing_Table". That is, the class name is the table name, with the first letter upper case, with a suffix '_Table'. This suffix can be changed by setting the $class_suffix property. The file containing a subclass definition is the subclass name with a php extension, e.g., 'Thing_Table.php'. The object instantiated from that subclass is the same as the table name, with no suffix, e.g., 'thing'.

To generate the code for all of the tables in a database named $database, instantiate a MDB2 or DB object named $db that connects to the database of interest, and execute the following code:

  1.      $generator = new DB_Table_Generator($db$database);
  2.      $generator->class_write_path = $class_write_path;
  3.      $generator->generateTableClassFiles();
  4.      $generator->generateDatabaseFile();
Here $class_write_path should be the path (without a trailing separator) to a directory in which all of the code should be written. If this directory does not exist, it will be created. If the directory does already exist, exising files will not be overwritten. If $class_write_path is not set (i.e., if this line is omitted) all the code will be written to the current directory. If ->generateDatabaseFile() is called, it must be called after ->generateTableClassFiles().

By default, ->generateTableClassFiles() and ->generateDatabaseFiles() generate code for all of the tables in the current database. To generate code for a specified list of tables, set the value of the public $tables property to a sequential list of table names before calling either of these methods. Code can be generated for three tables named 'table1', 'table2', and 'table3' as follows:

  1.      $generator = new DB_Table_Generator($db$database);
  2.      $generator->class_write_path = $class_write_path;
  3.      $generator->tables = array('table1''table2''table3');
  4.      $generator->generateTableClassFiles();
  5.      $generator->generateDatabaseFile();
If the $tables property is not set to a non-null value prior to calling ->generateTableClassFiles() then, by default, the database is queried for a list of all table names, by calling the ->getTableNames() method from within ->generateTableClassFiles().

PHP version 4 and 5



[ Top ]


Class Variables

$auto_inc_col = array()

[line 333]

Array of auto_increment column names

Array $this->auto_inc_col[table_name] = auto-increment column

  • Access: public

Type:   array


[ Top ]

$class_include_path =  ''

[line 299]

Include path to subclass definition files from database file

Used to create require_once statements in the Database.php file, which is in the same directory as the class definition files. Leave as empty string if your PHP include_path contains ".". The value should not include a trailing "/", which is added automatically to values other than the empty string.

  • Access: public

Type:   string


[ Top ]

$class_suffix =  "_Table"

[line 275]

Suffix to add to table names to obtain corresponding class names
  • Access: public

Type:   string


[ Top ]

$class_write_path =  ''

[line 285]

Path to directory in which subclass definitions should be written

Value should not include a trailing "/".

  • Access: public

Type:   string


[ Top ]

$col = array()

[line 311]

Array of column definitions

Array $this->col[table_name][column_name] = column definition. Column definition is an array with the same format as the $col property of a DB_Table object

  • Access: public

Type:   array


[ Top ]

$error =  null

[line 243]

If there is an error on instantiation, this captures that error.

This property is used only for errors encountered in the constructor at instantiation time. To check if there was an instantiation error...

  1.      $obj =new DB_Table_Generator();
  2.      if ($obj->error{
  3.          // ... error handling code here ...
  4.      }

  • Access: public

Type:   object PEAR_Error


[ Top ]

$extends =  'DB_Table'

[line 259]

Class being extended (DB_Table or generic subclass)
  • Access: public

Type:   string


[ Top ]

$extends_file =  'DB/Table.php'

[line 267]

Path to definition of the class $this->extends
  • Access: public

Type:   string


[ Top ]

$idx = array()

[line 323]

Array of index/constraint definitions.

Array $this->idx[table_table][index_name] = Index definition. The index definition is an array with the same format as the DB_Table $idx property property array.

  • Access: public

Type:   array


[ Top ]

$idxname_format =  '%s'

[line 353]

MDB2 'idxname_format' option, format of index names

For use in printf() formatting. Use '%s' to use index names as returned by getTableConstraints/Indexes, and '%s_idx' to add an '_idx' suffix. For MySQL, use the default value '%'.

  • Access: public

Type:   string


[ Top ]

$name =  null

[line 209]

Name of the database
  • Access: public

Type:   string


[ Top ]

$primary_key = array()

[line 341]

Array of primary keys
  • Access: public

Type:   array


[ Top ]

$tables = array()

[line 251]

Numerical array of table name strings
  • Access: public

Type:   array


[ Top ]



Method Detail

DB_Table_Generator (Constructor)   [line 372]

object DB_Table_Generator DB_Table_Generator( object &$db, string $name)

Constructor

If an error is encountered during instantiation, the error message is stored in the $this->error property of the resulting object. See $error property docblock for a discussion of error handling.

  • Access: public

Parameters:

object   &$db     DB/MDB2 database connection object
string   $name     database name string

[ Top ]

buildTableClass   [line 807]

string buildTableClass( string $table, [string $indent = ''])

Returns one skeleton DB_Table subclass definition, as php code

The returned subclass definition string contains values for the $col (column), $idx (index) and $auto_inc_col properties, with no method definitions.

  • Return: skeleton DB_Table subclass definition
  • Access: public

Parameters:

string   $table     name of table
string   $indent     string of whitespace for base indentation

[ Top ]

buildTableClasses   [line 956]

mixed buildTableClasses( )

Returns a string containing all table class definitions in one file

The returned string contains the contents of a single php file with definitions of DB_Table subclasses associated with all of the tables in $this->tables. If $this->tables is initially null, method $this->getTableNames() is called internally to generate a list of table names.

The returned string includes the opening and closing <?php and ?> script elements, and the require_once line needed to include the $this->extend_class (i.e., DB_Table or a subclass) that is being extended. To use, write this string to a new php file.

Usage:

  1.      $generator = new DB_Table_Generator($db$database);
  2.      echo $generator->buildTablesClasses();

  • Return: a string with all table class definitions, PEAR Error on failure
  • Access: public

[ Top ]

classFileName   [line 1323]

string classFileName( string $class_name)

Returns the path to a file containing a class definition

Appends '.php' to class name.

  • Return: file name
  • Access: public

Parameters:

string   $class_name     name of class

[ Top ]

className   [line 1285]

string className( string $table)

Convert a table name into a class name

Converts all non-alphanumeric characters to '_', capitalizes first letter, and adds $this->class_suffix to end. Override this if you want something else.

  • Return: class name;
  • Access: public

Parameters:

string   $table     name of table

[ Top ]

generateDatabaseFile   [line 1074]

mixed generateDatabaseFile( [string $object_name = null])

Writes a file to instantiate Table and Database objects

After successful completion, a file named 'Database.php' will be have been created in the $this->class_write_path directory. This file should normally be included in application php scripts. It can be renamed by the user.

Usage:

  1.      $generator = new DB_Table_Generator($db$database);
  2.      $generator->generateTableClassFiles();
  3.      $generator->generateDatabaseFile();

  • Return: true on success, PEAR Error on failure
  • Access: public

Parameters:

string   $object_name     variable name for DB_Table_Database object

[ Top ]

generateTableClassFiles   [line 998]

mixed generateTableClassFiles( )

Writes all table class definitions to separate files

Usage:

  1.      $generator = new DB_Table_Generator($db$database);
  2.      $generator->generateTableClassFiles();

  • Return: true on success, PEAR Error on failure
  • Access: public

[ Top ]

getTableDefinition   [line 519]

mixed getTableDefinition( string $table)

Gets column and index definitions by querying database

Upon return, column definitions are stored in $this->col[$table], and index definitions in $this->idx[$table].

Calls DB/MDB2::tableInfo() for column definitions, and uses the DB_Table_Manager class to obtain index definitions.

  • Return: true on success, PEAR Error on failure
  • Access: public

Parameters:

string   $table     name of table

[ Top ]

getTableNames   [line 462]

mixed getTableNames( )

Gets a list of tables from the database

Upon successful completion, names are stored in the $this->tables array. If an error is encountered, a PEAR Error is returned, and $this->tables is reset to null.

  • Return: true on success, PEAR Error on failure
  • Access: public

[ Top ]

setErrorMessage   [line 437]

void setErrorMessage( mixed $code, [string $message = null])

Overwrites one or more error messages, e.g., to internationalize them.
  • Access: public

Parameters:

mixed   $code     If string, the error message with code $code will be overwritten by $message. If array, each key is a code and each value is a new message.
string   $message     Only used if $key is not an array.

[ Top ]

tableName   [line 1305]

string tableName( string $table)

Returns a valid variable name from a table name

Converts all non-alphanumeric characters to '_'. Override this if you want something else.

  • Return: variable name;
  • Access: public

Parameters:

string   $table     name of table

[ Top ]

throwError   [line 407]

object PEAR_Error &throwError( string $code, [string $extra = null])

Specialized version of throwError() modeled on PEAR_Error.

Throws a PEAR_Error with a DB_Table_Generator error message based on a DB_Table_Generator constant error code.

  • Access: public

Parameters:

string   $code     A DB_Table_Generator error code constant.
string   $extra     Extra text for the error (in addition to the regular error message).

[ Top ]


Documentation generated on Thu, 25 Dec 2008 15:00:22 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.