Class: DB_Table_Generator
Source Location: /DB_Table-1.5.6/DB/Table/Generator.php
class DB_Table_Generator - Generates DB_Table subclass skeleton code
Author(s):
Version:
|
|
|
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:
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:
$generator->tables = array ('table1', 'table2', 'table3');
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
Class Variables
$auto_inc_col = array()
[line 333]
Array of auto_increment column names Array $this->auto_inc_col[table_name] = auto-increment column
$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.
$class_suffix = "_Table"
[line 275]
Suffix to add to table names to obtain corresponding class names
$class_write_path = ''
[line 285]
Path to directory in which subclass definitions should be written Value should not include a trailing "/".
$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
$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...
// ... error handling code here ...
}
$extends = 'DB_Table'
[line 259]
Class being extended (DB_Table or generic subclass)
$extends_file = 'DB/Table.php'
[line 267]
Path to definition of the class $this->extends
$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.
$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 '%'.
$name = null
[line 209]
Name of the database
$primary_key = array()
[line 341]
Array of primary keys
$tables = array()
[line 251]
Numerical array of table name strings
Method Detail
DB_Table_Generator (Constructor) [line 372]
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.
Parameters:
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.
Parameters:
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:
echo $generator->buildTablesClasses();
classFileName [line 1323]
string classFileName(
string
$class_name)
|
|
Returns the path to a file containing a class definition Appends '.php' to class name.
Parameters:
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.
Parameters:
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:
Parameters:
generateTableClassFiles [line 998]
mixed generateTableClassFiles(
)
|
|
Writes all table class definitions to separate files Usage:
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.
Parameters:
getTableNames [line 462]
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.
setErrorMessage [line 437]
void setErrorMessage(
mixed
$code, [string
$message = null])
|
|
Overwrites one or more error messages, e.g., to internationalize them.
Parameters:
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.
Parameters:
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.
Parameters:
Documentation generated on Mon, 11 Mar 2019 15:28:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|
|