Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineCreateModelTablesTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineCreateModelTablesTask.class.php

Revision 23810, 2.8 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) Jonathan H. Wage <jonwage@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
12
13 /**
14  * Create tables for specified list of models
15  *
16  * @package    symfony
17  * @subpackage doctrine
18  * @author     Jonathan H. Wage <jonwage@gmail.com>
19  * @version    SVN: $Id$
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->aliases = array();
35     $this->namespace = 'doctrine';
36     $this->name = 'create-model-tables';
37     $this->briefDescription = 'Drop and recreate tables for specified models.';
38
39     $this->detailedDescription = <<<EOF
40 The [doctrine:create-model-tables|INFO] Drop and recreate tables for specified models:
41
42   [./symfony doctrine:create-model-tables User|INFO]
43 EOF;
44   }
45
46   protected function execute($arguments = array(), $options = array())
47   {
48     $databaseManager = new sfDatabaseManager($this->configuration);
49
50     $buildModel = new sfDoctrineBuildModelTask($this->dispatcher, $this->formatter);
51     $buildModel->setCommandApplication($this->commandApplication);
52     $buildModel->setConfiguration($this->configuration);
53     $ret = $buildModel->run();
54
55     $connections = array();
56     $models = $arguments['models'];
57     foreach ($models as $key => $model)
58     {
59       $model = trim($model);
60       $conn = Doctrine_Core::getTable($model)->getConnection();
61       $connections[$conn->getName()][] = $model;
62     }
63
64     foreach ($connections as $name => $models)
65     {
66       $this->logSection('doctrine', 'dropping model tables for connection "'.$name.'"');
67
68       $conn = Doctrine_Manager::getInstance()->getConnection($name);
69       $models = $conn->unitOfWork->buildFlushTree($models);
70       $models = array_reverse($models);
71
72       foreach ($models as $model)
73       {
74         $tableName = Doctrine_Core::getTable($model)->getOption('tableName');
75
76         $this->logSection('doctrine', 'dropping table "'.$tableName.'"');
77
78         try {
79           $conn->export->dropTable($tableName);
80         }
81         catch (Exception $e)
82         {
83           $this->logSection('doctrine', 'dropping table failed: '.$e->getMessage());
84         }
85       }
86
87       $this->logSection('doctrine', 'recreating tables for models');
88
89       Doctrine_Core::createTablesFromArray($models);
90     }
91   }
92 }
Note: See TracBrowser for help on using the browser.