Doctrine 1.2.4
Doctrine_Import_Mysql Class Reference

Inherits Doctrine_Import.

Public Member Functions

 databaseExists ($database)
 
 functionExists ($function)
 
 getConnection ()
 
 getModuleName ()
 
 importSchema ($directory, array $connections=array(), array $options=array())
 
 listDatabases ()
 
 listFunctions ()
 
 listSequences ($database=null)
 
 listTableColumns ($table)
 
 listTableConstraints ($table)
 
 listTableIndexes ($table)
 
 listTableRelations ($tableName)
 
 listTables ($database=null)
 
 listTableTriggers ($table)
 
 listTableViews ($table)
 
 listTriggers ($database=null)
 
 listUsers ()
 
 listViews ($database=null)
 
 sequenceExists ($sequence, $database=null)
 
 tableColumnExists ($column, $table)
 
 tableConstraintExists ($constraint, $table)
 
 tableExists ($table, $database=null)
 
 tableIndexExists ($index, $table)
 
 tableTriggerExists ($trigger, $table)
 
 tableViewExists ($view, $table)
 
 triggerExists ($trigger, $database=null)
 
 userExists ($user)
 
 viewExists ($view, $database=null)
 

Detailed Description

Definition at line 32 of file Mysql.php.

Member Function Documentation

Doctrine_Import::databaseExists (   $database)
inherited

checks if a database exists

Parameters
string$database
Returns
boolean

Definition at line 220 of file Import.php.

{
return in_array($database, $this->listDatabases());
}
Doctrine_Import::functionExists (   $function)
inherited

checks if a function exists

Parameters
string$function
Returns
boolean

Definition at line 231 of file Import.php.

{
return in_array($function, $this->listFunctions());
}
Doctrine_Connection_Module::getConnection ( )
inherited

getConnection returns the connection object this module uses

Returns
Doctrine_Connection

Definition at line 68 of file Module.php.

{
return $this->conn;
}
Doctrine_Connection_Module::getModuleName ( )
inherited

getModuleName returns the name of this module

Returns
string the name of this module

Definition at line 79 of file Module.php.

{
return $this->moduleName;
}
Doctrine_Import::importSchema (   $directory,
array  $connections = array(),
array  $options = array() 
)
inherited

importSchema

method for importing existing schema to Doctrine_Record classes

Parameters
string$directory
array$connectionsArray of connection names to generate models for
Returns
array the names of the imported classes

Definition at line 364 of file Import.php.

{
$classes = array();
foreach ($manager as $name => $connection) {
// Limit the databases to the ones specified by $connections.
// Check only happens if array is not empty
if ( ! empty($connections) && ! in_array($name, $connections)) {
continue;
}
$builder = new Doctrine_Import_Builder();
$builder->setTargetPath($directory);
$builder->setOptions($options);
$definitions = array();
foreach ($connection->import->listTables() as $table) {
$definition = array();
$definition['tableName'] = $table;
$definition['className'] = Doctrine_Inflector::classify(Doctrine_Inflector::tableize($table));
$definition['columns'] = $connection->import->listTableColumns($table);
$definition['connection'] = $connection->getName();
$definition['connectionClassName'] = $definition['className'];
try {
$definition['relations'] = array();
$relations = $connection->import->listTableRelations($table);
$relClasses = array();
foreach ($relations as $relation) {
$table = $relation['table'];
if (in_array($class, $relClasses)) {
$alias = $class . '_' . (count($relClasses) + 1);
} else {
$alias = $class;
}
$relClasses[] = $class;
$definition['relations'][$alias] = array(
'alias' => $alias,
'class' => $class,
'local' => $relation['local'],
'foreign' => $relation['foreign']
);
}
} catch (Exception $e) {}
$definitions[strtolower($definition['className'])] = $definition;
$classes[] = $definition['className'];
}
// Build opposite end of relationships
foreach ($definitions as $definition) {
$className = $definition['className'];
$relClasses = array();
foreach ($definition['relations'] as $alias => $relation) {
if (in_array($relation['class'], $relClasses) || isset($definitions[$relation['class']]['relations'][$className])) {
$alias = $className . '_' . (count($relClasses) + 1);
} else {
$alias = $className;
}
$relClasses[] = $relation['class'];
$definitions[strtolower($relation['class'])]['relations'][$alias] = array(
'alias' => $alias,
'class' => $className,
'local' => $relation['foreign'],
'foreign' => $relation['local']
);
}
}
// Build records
foreach ($definitions as $definition) {
$builder->buildRecord($definition);
}
}
return $classes;
}
Doctrine_Import::listDatabases ( )
inherited

lists all databases

Returns
array

Definition at line 46 of file Import.php.

{
if ( ! isset($this->sql['listDatabases'])) {
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
return $this->conn->fetchColumn($this->sql['listDatabases']);
}
Doctrine_Import::listFunctions ( )
inherited

lists all availible database functions

Returns
array

Definition at line 60 of file Import.php.

{
if ( ! isset($this->sql['listFunctions'])) {
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
return $this->conn->fetchColumn($this->sql['listFunctions']);
}
Doctrine_Import_Mysql::listSequences (   $database = null)

lists all database sequences

Parameters
string | null$database
Returns
array

Definition at line 49 of file Mysql.php.

{
$query = 'SHOW TABLES';
if ( ! is_null($database)) {
$query .= ' FROM ' . $database;
}
$tableNames = $this->conn->fetchColumn($query);
return array_map(array($this->conn->formatter, 'fixSequenceName'), $tableNames);
}
Doctrine_Import_Mysql::listTableColumns (   $table)

lists table constraints

Parameters
string$tabledatabase table name
Returns
array

Definition at line 140 of file Mysql.php.

{
$sql = 'DESCRIBE ' . $this->conn->quoteIdentifier($table, true);
$result = $this->conn->fetchAssoc($sql);
$description = array();
$columns = array();
foreach ($result as $key => $val) {
$val = array_change_key_case($val, CASE_LOWER);
$decl = $this->conn->dataDict->getPortableDeclaration($val);
$values = isset($decl['values']) ? $decl['values'] : array();
$val['default'] = $val['default'] == 'CURRENT_TIMESTAMP' ? null : $val['default'];
$description = array(
'name' => $val['field'],
'type' => $decl['type'][0],
'alltypes' => $decl['type'],
'ntype' => $val['type'],
'length' => $decl['length'],
'fixed' => (bool) $decl['fixed'],
'unsigned' => (bool) $decl['unsigned'],
'values' => $values,
'primary' => (strtolower($val['key']) == 'pri'),
'default' => $val['default'],
'notnull' => (bool) ($val['null'] != 'YES'),
'autoincrement' => (bool) (strpos($val['extra'], 'auto_increment') !== false),
);
if (isset($decl['scale'])) {
$description['scale'] = $decl['scale'];
}
$columns[$val['field']] = $description;
}
return $columns;
}
Doctrine_Import_Mysql::listTableConstraints (   $table)

lists table constraints

Parameters
string$tabledatabase table name
Returns
array

Definition at line 66 of file Mysql.php.

{
$keyName = 'Key_name';
$nonUnique = 'Non_unique';
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) == CASE_LOWER) {
$keyName = strtolower($keyName);
$nonUnique = strtolower($nonUnique);
} else {
$keyName = strtoupper($keyName);
$nonUnique = strtoupper($nonUnique);
}
}
$table = $this->conn->quoteIdentifier($table, true);
$query = 'SHOW INDEX FROM ' . $table;
$indexes = $this->conn->fetchAssoc($query);
$result = array();
foreach ($indexes as $indexData) {
if ( ! $indexData[$nonUnique]) {
if ($indexData[$keyName] !== 'PRIMARY') {
$index = $this->conn->formatter->fixIndexName($indexData[$keyName]);
} else {
$index = 'PRIMARY';
}
if ( ! empty($index)) {
$result[] = $index;
}
}
}
return $result;
}
Doctrine_Import_Mysql::listTableIndexes (   $table)

lists table constraints

Parameters
string$tabledatabase table name
Returns
array

Definition at line 185 of file Mysql.php.

{
$keyName = 'Key_name';
$nonUnique = 'Non_unique';
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) && ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_FIX_CASE)) {
if ($this->conn->getAttribute(Doctrine_Core::ATTR_FIELD_CASE) == CASE_LOWER) {
$keyName = strtolower($keyName);
$nonUnique = strtolower($nonUnique);
} else {
$keyName = strtoupper($keyName);
$nonUnique = strtoupper($nonUnique);
}
}
$table = $this->conn->quoteIdentifier($table, true);
$query = 'SHOW INDEX FROM ' . $table;
$indexes = $this->conn->fetchAssoc($query);
$result = array();
foreach ($indexes as $indexData) {
if ($indexData[$nonUnique] && ($index = $this->conn->formatter->fixIndexName($indexData[$keyName]))) {
$result[] = $index;
}
}
return $result;
}
Doctrine_Import_Mysql::listTableRelations (   $tableName)

lists table relations

Expects an array of this format to be returned with all the relationships in it where the key is the name of the foreign table, and the value is an array containing the local and foreign column name

Array ( [groups] => Array ( [local] => group_id [foreign] => id ) )

Parameters
string$tabledatabase table name
Returns
array

Definition at line 119 of file Mysql.php.

{
$relations = array();
$sql = "SELECT column_name, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.key_column_usage WHERE table_name = '" . $tableName . "' AND table_schema = '" . $this->conn->getDatabaseName() . "' and REFERENCED_COLUMN_NAME is not NULL";
$results = $this->conn->fetchAssoc($sql);
foreach ($results as $result)
{
$result = array_change_key_case($result, CASE_LOWER);
$relations[] = array('table' => $result['referenced_table_name'],
'local' => $result['column_name'],
'foreign' => $result['referenced_column_name']);
}
return $relations;
}
Doctrine_Import_Mysql::listTables (   $database = null)

lists tables

Parameters
string | null$database
Returns
array

Definition at line 219 of file Mysql.php.

{
return $this->conn->fetchColumn($this->sql['listTables']);
}
Doctrine_Import::listTableTriggers (   $table)
inherited

lists table triggers

Parameters
string$tabledatabase table name
Returns
array

Definition at line 169 of file Import.php.

{
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
Doctrine_Import::listTableViews (   $table)
inherited

lists table views

Parameters
string$tabledatabase table name
Returns
array

Definition at line 180 of file Import.php.

{
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
Doctrine_Import::listTriggers (   $database = null)
inherited

lists all database triggers

Parameters
string | null$database
Returns
array

Definition at line 75 of file Import.php.

{
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
Doctrine_Import::listUsers ( )
inherited

lists database users

Returns
array

Definition at line 190 of file Import.php.

{
if ( ! isset($this->sql['listUsers'])) {
throw new Doctrine_Import_Exception(__FUNCTION__ . ' not supported by this driver.');
}
return $this->conn->fetchColumn($this->sql['listUsers']);
}
Doctrine_Import_Mysql::listViews (   $database = null)

lists database views

Parameters
string | null$database
Returns
array

Definition at line 230 of file Mysql.php.

{
if (is_null($database)) {
$query = 'SELECT table_name FROM information_schema.VIEWS';
} else {
$query = sprintf($this->sql['listViews'], ' FROM ' . $database);
}
return $this->conn->fetchColumn($query);
}
Doctrine_Import::sequenceExists (   $sequence,
  $database = null 
)
inherited

checks if a sequence exists

Parameters
string$sequence
string | null$database
Returns
boolean

Definition at line 255 of file Import.php.

{
return in_array($sequence, $this->listSequences($database));
}
Doctrine_Import::tableColumnExists (   $column,
  $table 
)
inherited

checks if a table column exists

Parameters
string$column
string$tabledatabase table name
Returns
boolean

Definition at line 279 of file Import.php.

{
return in_array($column, $this->listTableColumns($table));
}
Doctrine_Import::tableConstraintExists (   $constraint,
  $table 
)
inherited

checks if a table constraint exists

Parameters
string$constraint
string$tabledatabase table name
Returns
boolean

Definition at line 267 of file Import.php.

{
return in_array($constraint, $this->listTableConstraints($table));
}
Doctrine_Import::tableExists (   $table,
  $database = null 
)
inherited

checks if a table exists

Parameters
string$table
string | null$database
Returns
boolean

Definition at line 303 of file Import.php.

{
return in_array($table, $this->listTables($database));
}
Doctrine_Import::tableIndexExists (   $index,
  $table 
)
inherited

checks if a table index exists

Parameters
string$index
string$tabledatabase table name
Returns
boolean

Definition at line 291 of file Import.php.

{
return in_array($index, $this->listTableIndexes($table));
}
Doctrine_Import::tableTriggerExists (   $trigger,
  $table 
)
inherited

checks if a table trigger exists

Parameters
string$trigger
string$tabledatabase table name
Returns
boolean

Definition at line 315 of file Import.php.

{
return in_array($trigger, $this->listTableTriggers($table));
}
Doctrine_Import::tableViewExists (   $view,
  $table 
)
inherited

checks if a table view exists

Parameters
string$view
string$tabledatabase table name
Returns
boolean

Definition at line 327 of file Import.php.

{
return in_array($view, $this->listTableViews($table));
}
Doctrine_Import::triggerExists (   $trigger,
  $database = null 
)
inherited

checks if a trigger exists

Parameters
string$trigger
string | null$database
Returns
boolean

Definition at line 243 of file Import.php.

{
return in_array($trigger, $this->listTriggers($database));
}
Doctrine_Import::userExists (   $user)
inherited

checks if a user exists

Parameters
string$user
Returns
boolean

Definition at line 338 of file Import.php.

{
return in_array($user, $this->listUsers());
}
Doctrine_Import::viewExists (   $view,
  $database = null 
)
inherited

checks if a view exists

Parameters
string$view
string | null$database
Returns
boolean

Definition at line 350 of file Import.php.

{
return in_array($view, $this->listViews($database));
}

The documentation for this class was generated from the following file: