Doctrine 1.2.4
Doctrine_Export_Mysql Class Reference

Inherits Doctrine_Export.

Public Member Functions

 alterTable ($name, array $changes, $check=false)
 
 alterTableSql ($name, array $changes, $check=false)
 
 createConstraint ($table, $name, $definition)
 
 createConstraintSql ($table, $name, $definition)
 
 createDatabase ($database)
 
 createDatabaseSql ($name)
 
 createForeignKey ($table, array $definition)
 
 createForeignKeySql ($table, array $definition)
 
 createIndex ($table, $name, array $definition)
 
 createIndexSql ($table, $name, array $definition)
 
 createSequence ($sequenceName, $start=1, array $options=array())
 
 createSequenceSql ($seqName, $start=1, array $options=array())
 
 createTable ($name, array $fields, array $options=array())
 
 createTableSql ($name, array $fields, array $options=array())
 
 dropConstraint ($table, $name, $primary=false)
 
 dropDatabase ($database)
 
 dropDatabaseSql ($name)
 
 dropForeignKey ($table, $name)
 
 dropIndex ($table, $name)
 
 dropIndexSql ($table, $name)
 
 dropSequence ($sequenceName)
 
 dropSequenceSql ($sequenceName)
 
 dropTable ($table)
 
 dropTableSql ($table)
 
 exportClasses (array $classes)
 
 exportClassesSql (array $classes)
 
 exportGeneratorsSql (Doctrine_Table $table)
 
 exportSchema ($directory=null)
 
 exportSql ($directory=null)
 
 exportTable (Doctrine_Table $table)
 
 getAdvancedForeignKeyOptions (array $definition)
 
 getAllGenerators (Doctrine_Table $table)
 
 getCharsetFieldDeclaration ($charset)
 
 getCheckDeclaration (array $definition)
 
 getCollationFieldDeclaration ($collation)
 
 getConnection ()
 
 getDeclaration ($name, array $field)
 
 getDefaultFieldDeclaration ($field)
 
 getFieldDeclarationList (array $fields)
 
 getForeignKeyBaseDeclaration (array $definition)
 
 getForeignKeyDeclaration (array $definition)
 
 getForeignKeyReferentialAction ($action)
 
 getIndexDeclaration ($name, array $definition)
 
 getIndexFieldDeclarationList (array $fields)
 
 getModuleName ()
 
 getNotNullFieldDeclaration (array $definition)
 
 getTemporaryTableQuery ()
 
 getUniqueFieldDeclaration ()
 

Detailed Description

Definition at line 34 of file Mysql.php.

Member Function Documentation

Doctrine_Export::alterTable (   $name,
array  $changes,
  $check = false 
)
inherited

alter an existing table (this method is implemented by the drivers)

Parameters
string$namename of the table that is intended to be changed.
array$changesassociative array that contains the details of each type of change that is intended to be performed. The types of changes that are currently supported are defined as follows:

name

New name for the table.

add

Associative array with the names of fields to be added as
 indexes of the array. The value of each entry of the array
 should be set to another associative array with the properties
 of the fields to be added. The properties of the fields should
 be the same as defined by the MDB2 parser.

remove

Associative array with the names of fields to be removed as indexes
 of the array. Currently the values assigned to each entry are ignored.
 An empty array should be used for future compatibility.

rename

Associative array with the names of fields to be renamed as indexes
 of the array. The value of each entry of the array should be set to
 another associative array with the entry named name with the new
 field name and the entry named Declaration that is expected to contain
 the portion of the field declaration already in DBMS specific SQL code
 as it is used in the CREATE TABLE statement.

change

Associative array with the names of the fields to be changed as indexes
 of the array. Keep in mind that if it is intended to change either the
 name of a field and any other properties, the change array entries
 should have the new names of the fields as array indexes.

The value of each entry of the array should be set to another associative
 array with the properties of the fields to that are meant to be changed as
 array entries. These entries should be assigned to the new values of the
 respective properties. The properties of the fields should be the same
 as defined by the MDB2 parser.

Example array( 'name' => 'userlist', 'add' => array( 'quota' => array( 'type' => 'integer', 'unsigned' => 1 ) ), 'remove' => array( 'file_limit' => array(), 'time_limit' => array() ), 'change' => array( 'name' => array( 'length' => '20', 'definition' => array( 'type' => 'text', 'length' => 20, ), ) ), 'rename' => array( 'sex' => array( 'name' => 'gender', 'definition' => array( 'type' => 'text', 'length' => 1, 'default' => 'M', ), ) ) )

Parameters
boolean$checkindicates whether the function should just check if the DBMS driver can perform the requested table alterations if the value is true or actually perform them otherwise.
Returns
void

Definition at line 616 of file Export.php.

{
$sql = $this->alterTableSql($name, $changes, $check);
if (is_string($sql) && $sql) {
$this->conn->execute($sql);
}
}
Doctrine_Export_Mysql::alterTableSql (   $name,
array  $changes,
  $check = false 
)

alter an existing table

Parameters
string$namename of the table that is intended to be changed.
array$changesassociative array that contains the details of each type of change that is intended to be performed. The types of changes that are currently supported are defined as follows:

name

New name for the table.

add

Associative array with the names of fields to be added as
 indexes of the array. The value of each entry of the array
 should be set to another associative array with the properties
 of the fields to be added. The properties of the fields should
 be the same as defined by the Metabase parser.

remove

Associative array with the names of fields to be removed as indexes
 of the array. Currently the values assigned to each entry are ignored.
 An empty array should be used for future compatibility.

rename

Associative array with the names of fields to be renamed as indexes
 of the array. The value of each entry of the array should be set to
 another associative array with the entry named name with the new
 field name and the entry named Declaration that is expected to contain
 the portion of the field declaration already in DBMS specific SQL code
 as it is used in the CREATE TABLE statement.

change

Associative array with the names of the fields to be changed as indexes
 of the array. Keep in mind that if it is intended to change either the
 name of a field and any other properties, the change array entries
 should have the new names of the fields as array indexes.

The value of each entry of the array should be set to another associative
 array with the properties of the fields to that are meant to be changed as
 array entries. These entries should be assigned to the new values of the
 respective properties. The properties of the fields should be the same
 as defined by the Metabase parser.

Example array( 'name' => 'userlist', 'add' => array( 'quota' => array( 'type' => 'integer', 'unsigned' => 1 ) ), 'remove' => array( 'file_limit' => array(), 'time_limit' => array() ), 'change' => array( 'name' => array( 'length' => '20', 'definition' => array( 'type' => 'text', 'length' => 20, ), ) ), 'rename' => array( 'sex' => array( 'name' => 'gender', 'definition' => array( 'type' => 'text', 'length' => 1, 'default' => 'M', ), ) ) )

Parameters
boolean$checkindicates whether the function should just check if the DBMS driver can perform the requested table alterations if the value is true or actually perform them otherwise.
Returns
boolean

Definition at line 387 of file Mysql.php.

{
if ( ! $name) {
throw new Doctrine_Export_Exception('no valid table name specified');
}
foreach ($changes as $changeName => $change) {
switch ($changeName) {
case 'add':
case 'remove':
case 'change':
case 'rename':
case 'name':
break;
default:
throw new Doctrine_Export_Exception('change type "' . $changeName . '" not yet supported');
}
}
if ($check) {
return true;
}
$query = '';
if ( ! empty($changes['name'])) {
$change_name = $this->conn->quoteIdentifier($changes['name']);
$query .= 'RENAME TO ' . $change_name;
}
if ( ! empty($changes['add']) && is_array($changes['add'])) {
foreach ($changes['add'] as $fieldName => $field) {
if ($query) {
$query.= ', ';
}
$query.= 'ADD ' . $this->getDeclaration($fieldName, $field);
}
}
if ( ! empty($changes['remove']) && is_array($changes['remove'])) {
foreach ($changes['remove'] as $fieldName => $field) {
if ($query) {
$query .= ', ';
}
$fieldName = $this->conn->quoteIdentifier($fieldName);
$query .= 'DROP ' . $fieldName;
}
}
$rename = array();
if ( ! empty($changes['rename']) && is_array($changes['rename'])) {
foreach ($changes['rename'] as $fieldName => $field) {
$rename[$field['name']] = $fieldName;
}
}
if ( ! empty($changes['change']) && is_array($changes['change'])) {
foreach ($changes['change'] as $fieldName => $field) {
if ($query) {
$query.= ', ';
}
if (isset($rename[$fieldName])) {
$oldFieldName = $rename[$fieldName];
unset($rename[$fieldName]);
} else {
$oldFieldName = $fieldName;
}
$oldFieldName = $this->conn->quoteIdentifier($oldFieldName, true);
$query .= 'CHANGE ' . $oldFieldName . ' '
. $this->getDeclaration($fieldName, $field['definition']);
}
}
if ( ! empty($rename) && is_array($rename)) {
foreach ($rename as $renameName => $renamedField) {
if ($query) {
$query.= ', ';
}
$field = $changes['rename'][$renamedField];
$renamedField = $this->conn->quoteIdentifier($renamedField, true);
$query .= 'CHANGE ' . $renamedField . ' '
. $this->getDeclaration($field['name'], $field['definition']);
}
}
if ( ! $query) {
return false;
}
$name = $this->conn->quoteIdentifier($name, true);
return 'ALTER TABLE ' . $name . ' ' . $query;
}
Doctrine_Export::createConstraint (   $table,
  $name,
  $definition 
)
inherited

create a constraint on a table

Parameters
string$tablename of the table on which the constraint is to be created
string$namename of the constraint to be created
array$definitionassociative array that defines properties of the constraint to be created. Currently, only one property named FIELDS is supported. This property is also an associative with the names of the constraint fields as array constraints. Each entry of this array is set to another type of associative array that specifies properties of the constraint that are specific to each field.

Example array( 'fields' => array( 'user_name' => array(), 'last_login' => array() ) )

Returns
void

Definition at line 376 of file Export.php.

{
$sql = $this->createConstraintSql($table, $name, $definition);
return $this->conn->exec($sql);
}
Doctrine_Export::createConstraintSql (   $table,
  $name,
  $definition 
)
inherited

create a constraint on a table

Parameters
string$tablename of the table on which the constraint is to be created
string$namename of the constraint to be created
array$definitionassociative array that defines properties of the constraint to be created. Currently, only one property named FIELDS is supported. This property is also an associative with the names of the constraint fields as array constraints. Each entry of this array is set to another type of associative array that specifies properties of the constraint that are specific to each field.

Example array( 'fields' => array( 'user_name' => array(), 'last_login' => array() ) )

Returns
void

Definition at line 404 of file Export.php.

{
$table = $this->conn->quoteIdentifier($table);
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name));
$query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $name;
if (isset($definition['primary']) && $definition['primary']) {
$query .= ' PRIMARY KEY';
} elseif (isset($definition['unique']) && $definition['unique']) {
$query .= ' UNIQUE';
}
$fields = array();
foreach (array_keys($definition['fields']) as $field) {
$fields[] = $this->conn->quoteIdentifier($field, true);
}
$query .= ' ('. implode(', ', $fields) . ')';
return $query;
}
Doctrine_Export::createDatabase (   $database)
inherited

create a new database (this method is implemented by the drivers)

Parameters
string$namename of the database that should be created
Returns
void

Definition at line 188 of file Export.php.

{
$this->conn->execute($this->createDatabaseSql($database));
}
Doctrine_Export_Mysql::createDatabaseSql (   $name)

createDatabaseSql

Parameters
string$name
Returns
void

Definition at line 63 of file Mysql.php.

{
return 'CREATE DATABASE ' . $this->conn->quoteIdentifier($name, true);
}
Doctrine_Export::createForeignKey (   $table,
array  $definition 
)
inherited

createForeignKey

Parameters
string$tablename of the table on which the foreign key is to be created
array$definitionassociative array that defines properties of the foreign key to be created.
Returns
string

Definition at line 520 of file Export.php.

{
$sql = $this->createForeignKeySql($table, $definition);
return $this->conn->execute($sql);
}
Doctrine_Export::createForeignKeySql (   $table,
array  $definition 
)
inherited

createForeignKeySql

Parameters
string$tablename of the table on which the foreign key is to be created
array$definitionassociative array that defines properties of the foreign key to be created.
Returns
string

Definition at line 505 of file Export.php.

{
$table = $this->conn->quoteIdentifier($table);
$query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclaration($definition);
return $query;
}
Doctrine_Export::createIndex (   $table,
  $name,
array  $definition 
)
inherited

Get the stucture of a field into an array

Parameters
string$tablename of the table on which the index is to be created
string$namename of the index to be created
array$definitionassociative array that defines properties of the index to be created. Currently, only one property named FIELDS is supported. This property is also an associative with the names of the index fields as array indexes. Each entry of this array is set to another type of associative array that specifies properties of the index that are specific to each field.

Currently, only the sorting property is supported. It should be used to define the sorting direction of the index. It may be set to either ascending or descending.

Not all DBMS support index sorting direction configuration. The DBMS drivers of those that do not support it ignore this property. Use the function supports() to determine whether the DBMS driver can manage indexes.

Example array( 'fields' => array( 'user_name' => array( 'sorting' => 'ascending' ), 'last_login' => array() ) )

Returns
void

Definition at line 456 of file Export.php.

{
return $this->conn->execute($this->createIndexSql($table, $name, $definition));
}
Doctrine_Export_Mysql::createIndexSql (   $table,
  $name,
array  $definition 
)

Get the stucture of a field into an array

Author
Leoncx
Parameters
string$tablename of the table on which the index is to be created
string$namename of the index to be created
array$definitionassociative array that defines properties of the index to be created. Currently, only one property named FIELDS is supported. This property is also an associative with the names of the index fields as array indexes. Each entry of this array is set to another type of associative array that specifies properties of the index that are specific to each field.

Currently, only the sorting property is supported. It should be used to define the sorting direction of the index. It may be set to either ascending or descending.

Not all DBMS support index sorting direction configuration. The DBMS drivers of those that do not support it ignore this property. Use the function supports() to determine whether the DBMS driver can manage indexes.

Example array( 'fields' => array( 'user_name' => array( 'sorting' => 'ASC' 'length' => 10 ), 'last_login' => array() ) )

Exceptions
PDOException
Returns
void

Definition at line 589 of file Mysql.php.

{
$table = $table;
$table = $this->conn->quoteIdentifier($table, true);
$name = $this->conn->formatter->getIndexName($name);
$name = $this->conn->quoteIdentifier($name);
$type = '';
if (isset($definition['type'])) {
switch (strtolower($definition['type'])) {
case 'fulltext':
case 'unique':
$type = strtoupper($definition['type']) . ' ';
break;
default:
'Unknown type ' . $definition['type'] . ' for index ' . $name . ' in table ' . $table
);
}
}
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
return $query;
}
Doctrine_Export_Mysql::createSequence (   $sequenceName,
  $start = 1,
array  $options = array() 
)

create sequence

Parameters
string$sequenceNamename of the sequence to be created
string$startstart value of the sequence; default is 1
array$optionsAn associative array of table options: array( 'comment' => 'Foo', 'charset' => 'utf8', 'collate' => 'utf8_unicode_ci', 'type' => 'innodb', );
Returns
boolean

Definition at line 493 of file Mysql.php.

{
$sequenceName = $this->conn->quoteIdentifier($sequenceName, true);
$seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine_Core::ATTR_SEQCOL_NAME), true);
$optionsStrings = array();
if (isset($options['comment']) && ! empty($options['comment'])) {
$optionsStrings['comment'] = 'COMMENT = ' . $this->conn->quote($options['comment'], 'string');
}
if (isset($options['charset']) && ! empty($options['charset'])) {
$optionsStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
if (isset($options['collate'])) {
$optionsStrings['charset'] .= ' COLLATE ' . $options['collate'];
}
}
$type = false;
if (isset($options['type'])) {
$type = $options['type'];
} else {
$type = $this->conn->getAttribute(Doctrine_Core::ATTR_DEFAULT_TABLE_TYPE);
}
if ($type) {
$optionsStrings[] = 'ENGINE = ' . $type;
}
try {
$query = 'CREATE TABLE ' . $sequenceName
. ' (' . $seqcolName . ' BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
. $seqcolName . ')) ' . implode($optionsStrings, ' ');
$res = $this->conn->exec($query);
throw new Doctrine_Export_Exception('could not create sequence table');
}
if ($start == 1 && $res == 1)
return true;
$query = 'INSERT INTO ' . $sequenceName
. ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')';
$res = $this->conn->exec($query);
if ($res == 1)
return true;
// Handle error
try {
$result = $this->conn->exec('DROP TABLE ' . $sequenceName);
throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
}
}
Doctrine_Export::createSequenceSql (   $seqName,
  $start = 1,
array  $options = array() 
)
inherited

return RDBMS specific create sequence statement (this method is implemented by the drivers)

Exceptions
Doctrine_Connection_Exceptionif something fails at database level
Parameters
string$seqNamename of the sequence to be created
string$startstart value of the sequence; default is 1
array$optionsAn associative array of table options: array( 'comment' => 'Foo', 'charset' => 'utf8', 'collate' => 'utf8_unicode_ci', );
Returns
string

Definition at line 350 of file Export.php.

{
throw new Doctrine_Export_Exception('Create sequence not supported by this driver.');
}
Doctrine_Export::createTable (   $name,
array  $fields,
array  $options = array() 
)
inherited

create a new table

Parameters
string$nameName of the database that should be created
array$fieldsAssociative array that contains the definition of each field of the new table
array$optionsAn associative array of table options:
See Also
Doctrine_Export::createTableSql()
Returns
void

Definition at line 294 of file Export.php.

{
// Build array of the primary keys if any of the individual field definitions
// specify primary => true
$count = 0;
foreach ($fields as $fieldName => $field) {
if (isset($field['primary']) && $field['primary']) {
if ($count == 0) {
$options['primary'] = array();
}
$count++;
$options['primary'][] = $fieldName;
}
}
$sql = (array) $this->createTableSql($name, $fields, $options);
foreach ($sql as $query) {
$this->conn->execute($query);
}
}
Doctrine_Export_Mysql::createTableSql (   $name,
array  $fields,
array  $options = array() 
)

create a new table

Parameters
string$nameName of the database that should be created
array$fieldsAssociative array that contains the definition of each field of the new table The indexes of the array entries are the names of the fields of the table an the array entry values are associative arrays like those that are meant to be passed with the field definitions to get[Type]Declaration() functions. array( 'id' => array( 'type' => 'integer', 'unsigned' => 1 'notnull' => 1 'default' => 0 ), 'name' => array( 'type' => 'text', 'length' => 12 ), 'password' => array( 'type' => 'text', 'length' => 12 ) );
array$optionsAn associative array of table options: array( 'comment' => 'Foo', 'charset' => 'utf8', 'collate' => 'utf8_unicode_ci', 'type' => 'innodb', );
Returns
void

Definition at line 117 of file Mysql.php.

{
if ( ! $name)
throw new Doctrine_Export_Exception('no valid table name specified');
if (empty($fields)) {
throw new Doctrine_Export_Exception('no fields specified for table "'.$name.'"');
}
$queryFields = $this->getFieldDeclarationList($fields);
// build indexes for all foreign key fields (needed in MySQL!!)
if (isset($options['foreignKeys'])) {
foreach ($options['foreignKeys'] as $fk) {
$local = $fk['local'];
$found = false;
if (isset($options['indexes'])) {
foreach ($options['indexes'] as $definition) {
if (is_string($definition['fields'])) {
// Check if index already exists on the column
$found = $found || ($local == $definition['fields']);
} else if (in_array($local, $definition['fields']) && count($definition['fields']) === 1) {
// Index already exists on the column
$found = true;
}
}
}
if (isset($options['primary']) && !empty($options['primary']) &&
in_array($local, $options['primary'])) {
// field is part of the PK and therefore already indexed
$found = true;
}
if ( ! $found) {
if (is_array($local)) {
foreach($local as $localidx) {
$options['indexes'][$localidx] = array('fields' => array($localidx => array()));
}
} else {
$options['indexes'][$local] = array('fields' => array($local => array()));
}
}
}
}
// add all indexes
if (isset($options['indexes']) && ! empty($options['indexes'])) {
// Case Insensitive checking for duplicate indexes...
$dupes = array();
foreach ($options['indexes'] as $key => $index) {
if (in_array(strtolower($key), $dupes)) {
unset($options['indexes'][$key]);
} else {
$dupes[] = strtolower($key);
}
}
unset($dupes);
foreach($options['indexes'] as $index => $definition) {
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
}
}
// attach all primary keys
if (isset($options['primary']) && ! empty($options['primary'])) {
$keyColumns = array_values($options['primary']);
$keyColumns = array_map(array($this->conn, 'quoteIdentifier'), $keyColumns);
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}
$query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields . ')';
$optionStrings = array();
if (isset($options['comment'])) {
$optionStrings['comment'] = 'COMMENT = ' . $this->conn->quote($options['comment'], 'text');
}
if (isset($options['charset'])) {
$optionStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
}
if (isset($options['collate'])) {
$optionStrings['collate'] = 'COLLATE ' . $options['collate'];
}
$type = false;
// get the type of the table
if (isset($options['type'])) {
$type = $options['type'];
} else {
$type = $this->conn->getAttribute(Doctrine_Core::ATTR_DEFAULT_TABLE_TYPE);
}
if ($type) {
$optionStrings[] = 'ENGINE = ' . $type;
}
if ( ! empty($optionStrings)) {
$query.= ' '.implode(' ', $optionStrings);
}
$sql[] = $query;
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $k => $definition) {
if (is_array($definition)) {
$sql[] = $this->createForeignKeySql($name, $definition);
}
}
}
return $sql;
}
Doctrine_Export_Mysql::dropConstraint (   $table,
  $name,
  $primary = false 
)

drop existing constraint

Parameters
string$tablename of table that should be used in method
string$namename of the constraint to be dropped
string$primaryhint if the constraint is primary
Returns
void

Definition at line 44 of file Mysql.php.

{
$table = $this->conn->quoteIdentifier($table);
if ( ! $primary) {
$name = 'CONSTRAINT ' . $this->conn->quoteIdentifier($name);
} else {
$name = 'PRIMARY KEY';
}
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP ' . $name);
}
Doctrine_Export::dropDatabase (   $database)
inherited

drop an existing database (this method is implemented by the drivers)

Parameters
string$namename of the database that should be dropped
Returns
void

Definition at line 57 of file Export.php.

{
foreach ((array) $this->dropDatabaseSql($database) as $query) {
$this->conn->execute($query);
}
}
Doctrine_Export_Mysql::dropDatabaseSql (   $name)

drop an existing database

Parameters
string$namename of the database that should be dropped
Returns
string

Definition at line 74 of file Mysql.php.

{
return array(
'SET FOREIGN_KEY_CHECKS = 0',
'DROP DATABASE ' . $this->conn->quoteIdentifier($name),
'SET FOREIGN_KEY_CHECKS = 1'
);
}
Doctrine_Export_Mysql::dropForeignKey (   $table,
  $name 
)

drop existing foreign key

Parameters
string$tablename of table that should be used in method
string$namename of the foreign key to be dropped
Returns
void

Definition at line 812 of file Mysql.php.

{
$table = $this->conn->quoteIdentifier($table);
$name = $this->conn->quoteIdentifier($this->conn->formatter->getForeignKeyName($name));
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name);
}
Doctrine_Export::dropIndex (   $table,
  $name 
)
inherited

drop existing index

Parameters
string$tablename of table that should be used in method
string$namename of the index to be dropped
Returns
void

Definition at line 107 of file Export.php.

{
return $this->conn->exec($this->dropIndexSql($table, $name));
}
Doctrine_Export_Mysql::dropIndexSql (   $table,
  $name 
)

drop existing index

Parameters
string$tablename of table that should be used in method
string$namename of the index to be dropped
Returns
void

Definition at line 785 of file Mysql.php.

{
$table = $this->conn->quoteIdentifier($table, true);
$name = $this->conn->quoteIdentifier($this->conn->formatter->getIndexName($name), true);
return 'DROP INDEX ' . $name . ' ON ' . $table;
}
Doctrine_Export::dropSequence (   $sequenceName)
inherited

dropSequenceSql drop existing sequence (this method is implemented by the drivers)

Exceptions
Doctrine_Connection_Exceptionif something fails at database level
Parameters
string$sequenceNamename of the sequence to be dropped
Returns
void

Definition at line 163 of file Export.php.

{
$this->conn->exec($this->dropSequenceSql($sequenceName));
}
Doctrine_Export::dropSequenceSql (   $sequenceName)
inherited

dropSequenceSql drop existing sequence

Exceptions
Doctrine_Connection_Exceptionif something fails at database level
Parameters
string$sequenceNamename of the sequence to be dropped
Returns
void

Definition at line 176 of file Export.php.

{
throw new Doctrine_Export_Exception('Drop sequence not supported by this driver.');
}
Doctrine_Export::dropTable (   $table)
inherited

dropTable drop an existing table

Parameters
string$tablename of table that should be dropped from the database
Returns
void

Definition at line 95 of file Export.php.

{
$this->conn->execute($this->dropTableSql($table));
}
Doctrine_Export_Mysql::dropTableSql (   $table)

dropTable

Parameters
string$tablename of table that should be dropped from the database
Exceptions
PDOException
Returns
void

Definition at line 799 of file Mysql.php.

{
$table = $this->conn->quoteIdentifier($table, true);
return 'DROP TABLE ' . $table;
}
Doctrine_Export::exportClasses ( array  $classes)
inherited

exportClasses method for exporting Doctrine_Record classes to a schema

FIXME: This function has ugly hacks in it to make sure sql is inserted in the correct order.

Exceptions
Doctrine_Connection_Exceptionif some error other than Doctrine_Core::ERR_ALREADY_EXISTS occurred during the create table operation
Parameters
array$classes
Returns
void

Definition at line 1203 of file Export.php.

{
$queries = $this->exportSortedClassesSql($classes);
foreach ($queries as $connectionName => $sql) {
$connection = Doctrine_Manager::getInstance()->getConnection($connectionName);
$connection->beginTransaction();
foreach ($sql as $query) {
try {
$connection->exec($query);
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine_Core::ERR_ALREADY_EXISTS) {
$connection->rollback();
throw new Doctrine_Export_Exception($e->getMessage() . '. Failing Query: ' . $query);
}
}
}
$connection->commit();
}
}
Doctrine_Export::exportClassesSql ( array  $classes)
inherited

exportClassesSql method for exporting Doctrine_Record classes to a schema

Exceptions
Doctrine_Connection_Exceptionif some error other than Doctrine_Core::ERR_ALREADY_EXISTS occurred during the create table operation
Parameters
array$classes
Returns
void

Definition at line 1237 of file Export.php.

{
$sql = array();
foreach ($models as $name) {
$record = new $name();
$table = $record->getTable();
$parents = $table->getOption('joinedParents');
foreach ($parents as $parent) {
$data = $table->getConnection()->getTable($parent)->getExportableFormat();
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
$sql = array_merge($sql, (array) $query);
}
// Don't export the tables with attribute EXPORT_NONE'
if ($table->getAttribute(Doctrine_Core::ATTR_EXPORT) === Doctrine_Core::EXPORT_NONE) {
continue;
}
$data = $table->getExportableFormat();
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
if (is_array($query)) {
$sql = array_merge($sql, $query);
} else {
$sql[] = $query;
}
if ($table->getAttribute(Doctrine_Core::ATTR_EXPORT) & Doctrine_Core::EXPORT_PLUGINS) {
$sql = array_merge($sql, $this->exportGeneratorsSql($table));
}
// DC-474: Remove dummy $record from repository to not pollute it during export
$table->getRepository()->evict($record->getOid());
unset($record);
}
$sql = array_unique($sql);
rsort($sql);
return $sql;
}
Doctrine_Export::exportGeneratorsSql ( Doctrine_Table  $table)
inherited

exportGeneratorsSql exports plugin tables for given table

Parameters
Doctrine_Table$tablethe table in which the generators belong to
Returns
array an array of sql strings

Definition at line 1321 of file Export.php.

{
$sql = array();
foreach ($this->getAllGenerators($table) as $name => $generator) {
$table = $generator->getTable();
// Make sure plugin has a valid table
if ($table instanceof Doctrine_Table) {
$data = $table->getExportableFormat();
$query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
$sql = array_merge($sql, (array) $query);
}
}
return $sql;
}
Doctrine_Export::exportSchema (   $directory = null)
inherited

exportSchema method for exporting Doctrine_Record classes to a schema

if the directory parameter is given this method first iterates recursively trhough the given directory in order to find any model classes

Then it iterates through all declared classes and creates tables for the ones that extend Doctrine_Record and are not abstract classes

Exceptions
Doctrine_Connection_Exceptionif some error other than Doctrine_Core::ERR_ALREADY_EXISTS occurred during the create table operation
Parameters
string$directoryoptional directory parameter
Returns
void

Definition at line 1092 of file Export.php.

{
if ($directory !== null) {
} else {
}
$this->exportClasses($models);
}
Doctrine_Export::exportSql (   $directory = null)
inherited

exportSql returns the sql for exporting Doctrine_Record classes to a schema

if the directory parameter is given this method first iterates recursively trhough the given directory in order to find any model classes

Then it iterates through all declared classes and creates tables for the ones that extend Doctrine_Record and are not abstract classes

Exceptions
Doctrine_Connection_Exceptionif some error other than Doctrine_Core::ERR_ALREADY_EXISTS occurred during the create table operation
Parameters
string$directoryoptional directory parameter
Returns
void

Definition at line 1356 of file Export.php.

{
if ($directory !== null) {
} else {
}
return $this->exportSortedClassesSql($models, false);
}
Doctrine_Export::exportTable ( Doctrine_Table  $table)
inherited

exportTable exports given table into database based on column and option definitions

Exceptions
Doctrine_Connection_Exceptionif some error other than Doctrine_Core::ERR_ALREADY_EXISTS occurred during the create table operation
Returns
boolean whether or not the export operation was successful false if table already existed in the database

Definition at line 1376 of file Export.php.

{
try {
$data = $table->getExportableFormat();
$this->conn->export->createTable($data['tableName'], $data['columns'], $data['options']);
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine_Core::ERR_ALREADY_EXISTS) {
throw $e;
}
}
}
Doctrine_Export_Mysql::getAdvancedForeignKeyOptions ( array  $definition)

getAdvancedForeignKeyOptions Return the FOREIGN KEY query section dealing with non-standard options as MATCH, INITIALLY DEFERRED, ON UPDATE, ...

Parameters
array$definition
Returns
string

Definition at line 763 of file Mysql.php.

{
$query = '';
if ( ! empty($definition['match'])) {
$query .= ' MATCH ' . $definition['match'];
}
if ( ! empty($definition['onUpdate'])) {
$query .= ' ON UPDATE ' . $this->getForeignKeyReferentialAction($definition['onUpdate']);
}
if ( ! empty($definition['onDelete'])) {
$query .= ' ON DELETE ' . $this->getForeignKeyReferentialAction($definition['onDelete']);
}
return $query;
}
Doctrine_Export::getAllGenerators ( Doctrine_Table  $table)
inherited

fetches all generators recursively for given table

Parameters
Doctrine_Table$tabletable object to retrieve the generators from
Returns
array an array of Doctrine_Record_Generator objects

Definition at line 1293 of file Export.php.

{
$generators = array();
foreach ($table->getGenerators() as $name => $generator) {
if ($generator === null) {
continue;
}
$generators[] = $generator;
$generatorTable = $generator->getTable();
if ($generatorTable instanceof Doctrine_Table) {
$generators = array_merge($generators, $this->getAllGenerators($generatorTable));
}
}
return $generators;
}
Doctrine_Export_Mysql::getCharsetFieldDeclaration (   $charset)

Returns a character set declaration.

Parameters
string$charsetA character set
Returns
string A character set declaration

Definition at line 738 of file Mysql.php.

{
return $this->conn->dataDict->getCharsetFieldDeclaration($charset);
}
Doctrine_Export::getCheckDeclaration ( array  $definition)
inherited

Obtain DBMS specific SQL code portion needed to set a CHECK constraint declaration to be used in statements like CREATE TABLE.

Parameters
array$definitioncheck definition
Returns
string DBMS specific SQL code portion needed to set a CHECK constraint

Definition at line 809 of file Export.php.

{
$constraints = array();
foreach ($definition as $field => $def) {
if (is_string($def)) {
$constraints[] = 'CHECK (' . $def . ')';
} else {
if (isset($def['min'])) {
$constraints[] = 'CHECK (' . $field . ' >= ' . $def['min'] . ')';
}
if (isset($def['max'])) {
$constraints[] = 'CHECK (' . $field . ' <= ' . $def['max'] . ')';
}
}
}
return implode(', ', $constraints);
}
Doctrine_Export_Mysql::getCollationFieldDeclaration (   $collation)

Returns a collation declaration.

Parameters
string$collationA collation
Returns
string A collation declaration

Definition at line 750 of file Mysql.php.

{
return $this->conn->dataDict->getCollationFieldDeclaration($collation);
}
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_Export_Mysql::getDeclaration (   $name,
array  $field 
)

Obtain DBMS specific SQL code portion needed to declare a generic type field to be used in statements like CREATE TABLE.

Parameters
string$namename the field to be declared.
array$fieldassociative array with the name of the properties of the field being declared as array indexes. Currently, the types of supported field properties are as follows:

length Integer value that determines the maximum length of the text field. If this argument is missing the field should be declared to have the longest length allowed by the DBMS.

default Text value to be used as default for this field.

notnull Boolean flag that indicates whether this field is constrained to not be set to null. charset Text value with the default CHARACTER SET for this field. collation Text value with the default COLLATION for this field. unique unique constraint check column check constraint

Returns
string DBMS specific SQL code portion that should be used to declare the specified field.

Definition at line 261 of file Mysql.php.

{
$default = $this->getDefaultFieldDeclaration($field);
$charset = (isset($field['charset']) && $field['charset']) ?
' ' . $this->getCharsetFieldDeclaration($field['charset']) : '';
$collation = (isset($field['collation']) && $field['collation']) ?
' ' . $this->getCollationFieldDeclaration($field['collation']) : '';
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
$unique = (isset($field['unique']) && $field['unique']) ?
' ' . $this->getUniqueFieldDeclaration() : '';
$check = (isset($field['check']) && $field['check']) ?
' ' . $field['check'] : '';
$comment = (isset($field['comment']) && $field['comment']) ?
" COMMENT " . $this->conn->quote($field['comment'], 'text') : '';
$method = 'get' . $field['type'] . 'Declaration';
try {
if (method_exists($this->conn->dataDict, $method)) {
return $this->conn->dataDict->$method($name, $field);
} else {
$dec = $this->conn->dataDict->getNativeDeclaration($field);
}
return $this->conn->quoteIdentifier($name, true)
. ' ' . $dec . $charset . $default . $notnull . $comment . $unique . $check . $collation;
} catch (Exception $e) {
throw new Doctrine_Exception('Around field ' . $name . ': ' . $e->getMessage() . "\n\n" . $e->getTraceAsString() . "\n\n");
}
}
Doctrine_Export_Mysql::getDefaultFieldDeclaration (   $field)

getDefaultDeclaration Obtain DBMS specific SQL code portion needed to set a default value declaration to be used in statements like CREATE TABLE.

Parameters
array$fieldfield definition array
Returns
string DBMS specific SQL code portion needed to set a default value

Definition at line 623 of file Mysql.php.

{
$default = '';
if (isset($field['default']) && ( ! isset($field['length']) || $field['length'] <= 255)) {
if ($field['default'] === '') {
$field['default'] = empty($field['notnull'])
? null : $this->valid_default_values[$field['type']];
if ($field['default'] === ''
&& ($this->conn->getAttribute(Doctrine_Core::ATTR_PORTABILITY) & Doctrine_Core::PORTABILITY_EMPTY_TO_NULL)
) {
$field['default'] = ' ';
}
}
// Proposed patch:
if ($field['type'] == 'enum' && $this->conn->getAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM)) {
$fieldType = 'varchar';
} else {
$fieldType = $field['type'];
}
$default = ' DEFAULT ' . (is_null($field['default'])
? 'NULL'
: $this->conn->quote($field['default'], $fieldType));
//$default = ' DEFAULT ' . $this->conn->quote($field['default'], $field['type']);
}
return $default;
}
Doctrine_Export::getFieldDeclarationList ( array  $fields)
inherited

Get declaration of a number of field in bulk

Parameters
array$fieldsa multidimensional associative array. The first dimension determines the field name, while the second dimension is keyed with the name of the properties of the field being declared as array indexes. Currently, the types of supported field properties are as follows:

length Integer value that determines the maximum length of the text field. If this argument is missing the field should be declared to have the longest length allowed by the DBMS.

default Text value to be used as default for this field.

notnull Boolean flag that indicates whether this field is constrained to not be set to null. charset Text value with the default CHARACTER SET for this field. collation Text value with the default COLLATION for this field. unique unique constraint

Returns
string

Definition at line 671 of file Export.php.

{
foreach ($fields as $fieldName => $field) {
$query = $this->getDeclaration($fieldName, $field);
$queryFields[] = $query;
}
return implode(', ', $queryFields);
}
Doctrine_Export::getForeignKeyBaseDeclaration ( array  $definition)
inherited

getForeignKeyBaseDeclaration Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint of a field declaration to be used in statements like CREATE TABLE.

Parameters
array$definition
Returns
string

Definition at line 1006 of file Export.php.

{
$sql = '';
if (isset($definition['name'])) {
$sql .= 'CONSTRAINT ' . $this->conn->quoteIdentifier($this->conn->formatter->getForeignKeyName($definition['name'])) . ' ';
}
$sql .= 'FOREIGN KEY (';
if ( ! isset($definition['local'])) {
throw new Doctrine_Export_Exception('Local reference field missing from definition.');
}
if ( ! isset($definition['foreign'])) {
throw new Doctrine_Export_Exception('Foreign reference field missing from definition.');
}
if ( ! isset($definition['foreignTable'])) {
throw new Doctrine_Export_Exception('Foreign reference table missing from definition.');
}
if ( ! is_array($definition['local'])) {
$definition['local'] = array($definition['local']);
}
if ( ! is_array($definition['foreign'])) {
$definition['foreign'] = array($definition['foreign']);
}
$sql .= implode(', ', array_map(array($this->conn, 'quoteIdentifier'), $definition['local']))
. ') REFERENCES '
. $this->conn->quoteIdentifier($definition['foreignTable']) . '('
. implode(', ', array_map(array($this->conn, 'quoteIdentifier'), $definition['foreign'])) . ')';
return $sql;
}
Doctrine_Export::getForeignKeyDeclaration ( array  $definition)
inherited

getForeignKeyDeclaration Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint of a field declaration to be used in statements like CREATE TABLE.

Parameters
array$definitionan associative array with the following structure: name optional constraint name

local the local field(s)

foreign the foreign reference field(s)

foreignTable the name of the foreign table

onDelete referential delete action

onUpdate referential update action

deferred deferred constraint checking

The onDelete and onUpdate keys accept the following values:

CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table.

SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.

NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary key value is not allowed to proceed if there is a related foreign key value in the referenced table.

RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as omitting the ON DELETE or ON UPDATE clause.

SET DEFAULT

Returns
string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint of a field declaration.

Definition at line 944 of file Export.php.

{
$sql = $this->getForeignKeyBaseDeclaration($definition);
$sql .= $this->getAdvancedForeignKeyOptions($definition);
return $sql;
}
Doctrine_Export::getForeignKeyReferentialAction (   $action)
inherited

getForeignKeyReferentialAction

returns given referential action in uppercase if valid, otherwise throws an exception

Exceptions
Doctrine_Exception_Exceptionif unknown referential action given
Parameters
string$actionforeign key referential action
stringforeign key referential action in uppercase

Definition at line 982 of file Export.php.

{
$upper = strtoupper($action);
switch ($upper) {
case 'CASCADE':
case 'SET NULL':
case 'NO ACTION':
case 'RESTRICT':
case 'SET DEFAULT':
return $upper;
break;
default:
throw new Doctrine_Export_Exception('Unknown foreign key referential action \'' . $upper . '\' given.');
}
}
Doctrine_Export_Mysql::getIndexDeclaration (   $name,
array  $definition 
)

Obtain DBMS specific SQL code portion needed to set an index declaration to be used in statements like CREATE TABLE.

Parameters
string$charsetname of the index
array$definitionindex definition
Returns
string DBMS specific SQL code portion needed to set an index

Definition at line 662 of file Mysql.php.

{
$name = $this->conn->formatter->getIndexName($name);
$type = '';
if (isset($definition['type'])) {
switch (strtolower($definition['type'])) {
case 'fulltext':
case 'unique':
$type = strtoupper($definition['type']) . ' ';
break;
default:
'Unknown type ' . $definition['type'] . ' for index ' . $name
);
}
}
if ( ! isset($definition['fields'])) {
throw new Doctrine_Export_Exception('No columns given for index ' . $name);
}
if ( ! is_array($definition['fields'])) {
$definition['fields'] = array($definition['fields']);
}
$query = $type . 'INDEX ' . $this->conn->quoteIdentifier($name);
$query .= ' (' . $this->getIndexFieldDeclarationList($definition['fields']) . ')';
return $query;
}
Doctrine_Export_Mysql::getIndexFieldDeclarationList ( array  $fields)

getIndexFieldDeclarationList Obtain DBMS specific SQL code portion needed to set an index declaration to be used in statements like CREATE TABLE.

Returns
string

Definition at line 700 of file Mysql.php.

{
$declFields = array();
foreach ($fields as $fieldName => $field) {
$fieldString = $this->conn->quoteIdentifier($fieldName);
if (is_array($field)) {
if (isset($field['length'])) {
$fieldString .= '(' . $field['length'] . ')';
}
if (isset($field['sorting'])) {
$sort = strtoupper($field['sorting']);
switch ($sort) {
case 'ASC':
case 'DESC':
$fieldString .= ' ' . $sort;
break;
default:
throw new Doctrine_Export_Exception('Unknown index sorting option given.');
}
}
} else {
$fieldString = $this->conn->quoteIdentifier($field);
}
$declFields[] = $fieldString;
}
return implode(', ', $declFields);
}
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_Export::getNotNullFieldDeclaration ( array  $definition)
inherited

getNotNullFieldDeclaration Obtain DBMS specific SQL code portion needed to set a NOT NULL declaration to be used in statements like CREATE TABLE.

Parameters
array$fieldfield definition array
Returns
string DBMS specific SQL code portion needed to set a default value

Definition at line 796 of file Export.php.

{
return (isset($definition['notnull']) && $definition['notnull']) ? ' NOT NULL' : '';
}
Doctrine_Export::getTemporaryTableQuery ( )
inherited

A method to return the required SQL string that fits between CREATE ... TABLE to create the table as a temporary table.

Should be overridden in driver classes to return the correct string for the specific database type.

The default is to return the string "TEMPORARY" - this will result in a SQL error for any database that does not support temporary tables, or that requires a different SQL command from "CREATE TEMPORARY TABLE".

Returns
string The string required to be placed between "CREATE" and "TABLE" to generate a temporary table, if possible.

Definition at line 897 of file Export.php.

{
return 'TEMPORARY';
}
Doctrine_Export::getUniqueFieldDeclaration ( )
inherited

Obtain DBMS specific SQL code portion needed to set the UNIQUE constraint of a field declaration to be used in statements like CREATE TABLE.

Returns
string DBMS specific SQL code portion needed to set the UNIQUE constraint of a field declaration.

Definition at line 1046 of file Export.php.

{
return 'UNIQUE';
}

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