Doctrine 1.2.4
Doctrine_Migration_Builder Class Reference

Inherits Doctrine_Builder.

Public Member Functions

 __construct ($migrationsPath=null)
 
 buildAddColumn ($tableName, $columnName, $column)
 
 buildAddIndex ($tableName, $indexName, $index)
 
 buildChangeColumn ($tableName, $columnName, $column)
 
 buildCreateForeignKey ($tableName, $definition)
 
 buildCreateTable ($tableData)
 
 buildDropForeignKey ($tableName, $definition)
 
 buildDropTable ($tableData)
 
 buildMigrationClass ($className, $fileName=null, $options=array(), $up=null, $down=null)
 
 buildRemoveColumn ($tableName, $columnName, $column)
 
 buildRemoveIndex ($tableName, $indexName, $index)
 
 generateMigrationClass ($className, $options=array(), $up=null, $down=null, $return=false)
 
 generateMigrationsFromDb ()
 
 generateMigrationsFromDiff (Doctrine_Migration_Diff $diff)
 
 generateMigrationsFromModels ($modelsPath=null, $modelLoading=null)
 
 getMigrationsPath ()
 
 setMigrationsPath ($path)
 
 varExport ($var)
 

Protected Member Functions

 loadTemplate ()
 

Detailed Description

Definition at line 34 of file Builder.php.

Constructor & Destructor Documentation

Doctrine_Migration_Builder::__construct (   $migrationsPath = null)

Instantiate new instance of the Doctrine_Migration_Builder class

$builder = new Doctrine_Migration_Builder('/path/to/migrations');

Returns
void

Definition at line 73 of file Builder.php.

{
if ($migrationsPath instanceof Doctrine_Migration) {
$this->setMigrationsPath($migrationsPath->getMigrationClassesDirectory());
$this->migration = $migrationsPath;
} else if (is_dir($migrationsPath)) {
$this->setMigrationsPath($migrationsPath);
$this->migration = new Doctrine_Migration($migrationsPath);
}
$this->loadTemplate();
}

Member Function Documentation

Doctrine_Migration_Builder::buildAddColumn (   $tableName,
  $columnName,
  $column 
)

Build the code for adding columns

Parameters
string$tableName
string$columnName
string$column
Returns
string $code

Definition at line 411 of file Builder.php.

{
$length = $column['length'];
$type = $column['type'];
unset($column['length'], $column['type']);
Doctrine_Migration_Builder::buildAddIndex (   $tableName,
  $indexName,
  $index 
)

Build the code for adding indexes

Parameters
string$tableName
string$indexName
string$index
Returns
sgtring $code

Definition at line 456 of file Builder.php.

{
Doctrine_Migration_Builder::buildChangeColumn (   $tableName,
  $columnName,
  $column 
)

Build the code for changing columns

Parameters
string$tableName
string$columnName
string$column
Returns
string $code

Definition at line 440 of file Builder.php.

{
$length = $column['length'];
$type = $column['type'];
unset($column['length'], $column['type']);
Doctrine_Migration_Builder::buildCreateForeignKey (   $tableName,
  $definition 
)

Build the code for creating foreign keys

Parameters
string$tableName
array$definition
Returns
string $code

Definition at line 347 of file Builder.php.

{
Doctrine_Migration_Builder::buildCreateTable (   $tableData)

Build the code for creating tables

Parameters
string$tableData
Returns
string $code

Definition at line 370 of file Builder.php.

{
$code = " \$this->createTable('" . $tableData['tableName'] . "', ";
$code .= $this->varExport($tableData['columns'], true) . ", ";
$optionsWeNeed = array('type', 'indexes', 'primary', 'collate', 'charset');
$options = array();
foreach ($optionsWeNeed as $option) {
if (isset($tableData['options'][$option])) {
$options[$option] = $tableData['options'][$option];
}
}
$code .= $this->varExport($options, true);
$code .= ");";
Doctrine_Migration_Builder::buildDropForeignKey (   $tableName,
  $definition 
)

Build the code for dropping foreign keys

Parameters
string$tableName
array$definition
Returns
string $code

Definition at line 359 of file Builder.php.

{
Doctrine_Migration_Builder::buildDropTable (   $tableData)

Build the code for dropping tables

Parameters
string$tableData
Returns
string $code

Definition at line 398 of file Builder.php.

{
Doctrine_Migration_Builder::buildMigrationClass (   $className,
  $fileName = null,
  $options = array(),
  $up = null,
  $down = null 
)

Build the code for a migration class

Parameters
string$classNameClass name to generate
string$fileNameFile name to write the class to
array$optionsOptions for the migration class
string$upThe code for the up function
string$downThe code for the down function
Returns
string $content The code for the generated class

Definition at line 527 of file Builder.php.

{
$extends = isset($options['extends']) ? $options['extends']:'Doctrine_Migration_Base';
$content = '<?php' . PHP_EOL;
$content .= sprintf(self::$tpl, $className,
$extends,
$up,
$down);
Doctrine_Migration_Builder::buildRemoveColumn (   $tableName,
  $columnName,
  $column 
)

Build the code for removing columns

Parameters
string$tableName
string$columnName
string$column
Returns
string $code

Definition at line 427 of file Builder.php.

{
Doctrine_Migration_Builder::buildRemoveIndex (   $tableName,
  $indexName,
  $index 
)

Build the code for removing indexes

Parameters
string$tableName
string$indexName
string$index
Returns
string $code

Definition at line 469 of file Builder.php.

{
Doctrine_Migration_Builder::generateMigrationClass (   $className,
  $options = array(),
  $up = null,
  $down = null,
  $return = false 
)

Generate a migration class

Parameters
string$classNameClass name to generate
array$optionsOptions for the migration class
string$upThe code for the up function
string$downThe code for the down function
boolean$returnWhether or not to return the code. If true return and false it writes the class to disk.
Returns
mixed

Definition at line 485 of file Builder.php.

{
$className = Doctrine_Inflector::urlize($className);
$className = str_replace('-', '_', $className);
$className = Doctrine_Inflector::classify($className);
if ($return || ! $this->getMigrationsPath()) {
return $this->buildMigrationClass($className, null, $options, $up, $down);
} else {
if ( ! $this->getMigrationsPath()) {
throw new Doctrine_Migration_Exception('You must specify the path to your migrations.');
}
$next = time() + $this->migration->getNextMigrationClassVersion();
$fileName = $next . '_' . Doctrine_Inflector::tableize($className) . $this->suffix;
$class = $this->buildMigrationClass($className, $fileName, $options, $up, $down);
$path = $this->getMigrationsPath() . DIRECTORY_SEPARATOR . $fileName;
if (class_exists($className) || file_exists($path)) {
$this->migration->loadMigrationClass($className);
return false;
}
file_put_contents($path, $class);
require_once($path);
$this->migration->loadMigrationClass($className);
return true;
Doctrine_Migration_Builder::generateMigrationsFromDb ( )

Generate a set of migration classes from the existing databases

Returns
void

Definition at line 268 of file Builder.php.

{
$directory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'tmp_doctrine_models';
Doctrine_Migration_Builder::generateMigrationsFromDiff ( Doctrine_Migration_Diff  $diff)

Generate migrations from a Doctrine_Migration_Diff instance

Parameters
Doctrine_Migration_Diff$diffInstance to generate changes from
Returns
array $changes Array of changes produced from the diff

Definition at line 143 of file Builder.php.

{
$changes = $diff->generateChanges();
$up = array();
$down = array();
if ( ! empty($changes['dropped_tables'])) {
foreach ($changes['dropped_tables'] as $tableName => $table) {
$up[] = $this->buildDropTable($table);
$down[] = $this->buildCreateTable($table);
}
}
if ( ! empty($changes['created_tables'])) {
foreach ($changes['created_tables'] as $tableName => $table) {
$up[] = $this->buildCreateTable($table);
$down[] = $this->buildDropTable($table);
}
}
if ( ! empty($changes['dropped_columns'])) {
foreach ($changes['dropped_columns'] as $tableName => $removedColumns) {
foreach ($removedColumns as $name => $column) {
$up[] = $this->buildRemoveColumn($tableName, $name, $column);
$down[] = $this->buildAddColumn($tableName, $name, $column);
}
}
}
if ( ! empty($changes['created_columns'])) {
foreach ($changes['created_columns'] as $tableName => $addedColumns) {
foreach ($addedColumns as $name => $column) {
$up[] = $this->buildAddColumn($tableName, $name, $column);
$down[] = $this->buildRemoveColumn($tableName, $name, $column);
}
}
}
if ( ! empty($changes['changed_columns'])) {
foreach ($changes['changed_columns'] as $tableName => $changedColumns) {
foreach ($changedColumns as $name => $column) {
$up[] = $this->buildChangeColumn($tableName, $name, $column);
}
}
}
if ( ! empty($up) || ! empty($down)) {
$up = implode("\n", $up);
$down = implode("\n", $down);
$className = 'Version' . $this->migration->getNextMigrationClassVersion();
$this->generateMigrationClass($className, array(), $up, $down);
}
$up = array();
$down = array();
if ( ! empty($changes['dropped_foreign_keys'])) {
foreach ($changes['dropped_foreign_keys'] as $tableName => $droppedFks) {
if ( ! empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) {
continue;
}
foreach ($droppedFks as $name => $foreignKey) {
$up[] = $this->buildDropForeignKey($tableName, $foreignKey);
$down[] = $this->buildCreateForeignKey($tableName, $foreignKey);
}
}
}
if ( ! empty($changes['dropped_indexes'])) {
foreach ($changes['dropped_indexes'] as $tableName => $removedIndexes) {
if ( ! empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) {
continue;
}
foreach ($removedIndexes as $name => $index) {
$up[] = $this->buildRemoveIndex($tableName, $name, $index);
$down[] = $this->buildAddIndex($tableName, $name, $index);
}
}
}
if ( ! empty($changes['created_foreign_keys'])) {
foreach ($changes['created_foreign_keys'] as $tableName => $createdFks) {
if ( ! empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) {
continue;
}
foreach ($createdFks as $name => $foreignKey) {
$up[] = $this->buildCreateForeignKey($tableName, $foreignKey);
$down[] = $this->buildDropForeignKey($tableName, $foreignKey);
}
}
}
if ( ! empty($changes['created_indexes'])) {
foreach ($changes['created_indexes'] as $tableName => $addedIndexes) {
if ( ! empty($changes['dropped_tables']) && isset($changes['dropped_tables'][$tableName])) {
continue;
}
foreach ($addedIndexes as $name => $index) {
if (isset($changes['created_tables'][$tableName]['options']['indexes'][$name])) {
continue;
}
$up[] = $this->buildAddIndex($tableName, $name, $index);
$down[] = $this->buildRemoveIndex($tableName, $name, $index);
}
}
}
if ( ! empty($up) || ! empty($down)) {
$up = implode("\n", $up);
$down = implode("\n", $down);
$className = 'Version' . $this->migration->getNextMigrationClassVersion();
$this->generateMigrationClass($className, array(), $up, $down);
}
Doctrine_Migration_Builder::generateMigrationsFromModels (   $modelsPath = null,
  $modelLoading = null 
)

Generate a set of migrations from a set of models

Parameters
string$modelsPathPath to models
string$modelLoadingWhat type of model loading to use when loading the models
Returns
boolean

Definition at line 288 of file Builder.php.

{
if ($modelsPath !== null) {
$models = Doctrine_Core::filterInvalidModels(Doctrine_Core::loadModels($modelsPath, $modelLoading));
} else {
}
$models = Doctrine_Core::initializeModels($models);
$foreignKeys = array();
foreach ($models as $model) {
$table = Doctrine_Core::getTable($model);
if ($table->getTableName() !== $this->migration->getTableName()) {
$export = $table->getExportableFormat();
$foreignKeys[$export['tableName']] = $export['options']['foreignKeys'];
$up = $this->buildCreateTable($export);
$down = $this->buildDropTable($export);
$className = 'Add' . Doctrine_Inflector::classify($export['tableName']);
$this->generateMigrationClass($className, array(), $up, $down);
}
}
if ( ! empty($foreignKeys)) {
$className = 'AddFks';
$up = array();
$down = array();
foreach ($foreignKeys as $tableName => $definitions) {
$tableForeignKeyNames[$tableName] = array();
foreach ($definitions as $definition) {
$up[] = $this->buildCreateForeignKey($tableName, $definition);
$down[] = $this->buildDropForeignKey($tableName, $definition);
}
}
$up = implode("\n", $up);
$down = implode("\n", $down);
if ($up || $down) {
$this->generateMigrationClass($className, array(), $up, $down);
}
}
Doctrine_Migration_Builder::getMigrationsPath ( )

Get the path where generated migration classes are written to

Returns
string the path where migration classes are stored and being generated

Definition at line 104 of file Builder.php.

{
return $this->migrationsPath;
}
Doctrine_Migration_Builder::loadTemplate ( )
protected

Loads the class template used for generating classes

Returns
void

Definition at line 114 of file Builder.php.

{
if (isset(self::$tpl)) {
return;
}
self::$tpl =<<<END
/**
* This class has been auto-generated by the Doctrine ORM Framework
*/
class %s extends %s
{
public function up()
{
%s
}
public function down()
{
%s
}
}
Doctrine_Migration_Builder::setMigrationsPath (   $path)

Set the path to write the generated migration classes

Parameters
stringpath the path where migration classes are stored and being generated
Returns
void

Definition at line 92 of file Builder.php.

{
$this->migrationsPath = $path;
}
Doctrine_Builder::varExport (   $var)
inherited

Special function for var_export() The normal code which is returned is malformed and does not follow Doctrine standards So we do some string replacing to clean it up

Parameters
string$var
Returns
void

Definition at line 43 of file Builder.php.

{
$export = var_export($var, true);
$export = str_replace("\n", PHP_EOL . str_repeat(' ', 50), $export);
$export = str_replace(' ', ' ', $export);
$export = str_replace('array (', 'array(', $export);
$export = str_replace('array( ', 'array(', $export);
$export = str_replace(',)', ')', $export);
$export = str_replace(', )', ')', $export);
$export = str_replace(' ', ' ', $export);
return $export;
}

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