| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php'); |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfDoctrineCreateModelTables extends sfDoctrineBaseTask |
|---|
| 22 |
{ |
|---|
| 23 |
protected function configure() |
|---|
| 24 |
{ |
|---|
| 25 |
$this->addArguments(array( |
|---|
| 26 |
new sfCommandArgument('models', sfCommandArgument::IS_ARRAY, 'The list of models', array()), |
|---|
| 27 |
)); |
|---|
| 28 |
|
|---|
| 29 |
$this->addOptions(array( |
|---|
| 30 |
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', 'frontend'), |
|---|
| 31 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), |
|---|
| 32 |
)); |
|---|
| 33 |
|
|---|
| 34 |
$this->namespace = 'doctrine'; |
|---|
| 35 |
$this->name = 'create-model-tables'; |
|---|
| 36 |
$this->briefDescription = 'Drop and recreate tables for specified models.'; |
|---|
| 37 |
|
|---|
| 38 |
$this->detailedDescription = <<<EOF |
|---|
| 39 |
The [doctrine:create-model-tables|INFO] Drop and recreate tables for specified models: |
|---|
| 40 |
|
|---|
| 41 |
[./symfony doctrine:create-model-tables User|INFO] |
|---|
| 42 |
EOF; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 46 |
{ |
|---|
| 47 |
$databaseManager = new sfDatabaseManager($this->configuration); |
|---|
| 48 |
|
|---|
| 49 |
$buildModel = new sfDoctrineBuildModelTask($this->dispatcher, $this->formatter); |
|---|
| 50 |
$buildModel->setCommandApplication($this->commandApplication); |
|---|
| 51 |
$buildModel->setConfiguration($this->configuration); |
|---|
| 52 |
$ret = $buildModel->run(); |
|---|
| 53 |
|
|---|
| 54 |
$connections = array(); |
|---|
| 55 |
$models = $arguments['models']; |
|---|
| 56 |
foreach ($models as $key => $model) |
|---|
| 57 |
{ |
|---|
| 58 |
$model = trim($model); |
|---|
| 59 |
$conn = Doctrine_Core::getTable($model)->getConnection(); |
|---|
| 60 |
$connections[$conn->getName()][] = $model; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
foreach ($connections as $name => $models) |
|---|
| 64 |
{ |
|---|
| 65 |
$this->logSection('doctrine', 'dropping model tables for connection "'.$name.'"'); |
|---|
| 66 |
|
|---|
| 67 |
$conn = Doctrine_Manager::getInstance()->getConnection($name); |
|---|
| 68 |
$models = $conn->unitOfWork->buildFlushTree($models); |
|---|
| 69 |
$models = array_reverse($models); |
|---|
| 70 |
|
|---|
| 71 |
foreach ($models as $model) |
|---|
| 72 |
{ |
|---|
| 73 |
$tableName = Doctrine_Core::getTable($model)->getOption('tableName'); |
|---|
| 74 |
|
|---|
| 75 |
$this->logSection('doctrine', 'dropping table "'.$tableName.'"'); |
|---|
| 76 |
|
|---|
| 77 |
try { |
|---|
| 78 |
$conn->export->dropTable($tableName); |
|---|
| 79 |
} |
|---|
| 80 |
catch (Exception $e) |
|---|
| 81 |
{ |
|---|
| 82 |
$this->logSection('doctrine', 'dropping table failed: '.$e->getMessage()); |
|---|
| 83 |
} |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
$this->logSection('doctrine', 'recreating tables for models'); |
|---|
| 87 |
|
|---|
| 88 |
Doctrine_Core::createTablesFromArray($models); |
|---|
| 89 |
} |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|