/**
  * Detects the differences between current connected database and $pDatabase
  * and updates the schema. This does not DROP tables.
  *
  * @param Database $pDatabase
  */
 public function updateSchema($pDatabase)
 {
     $diff = PropelDatabaseComparator::computeDiff($this->database, $pDatabase);
     $sql = $this->database->getPlatform()->getModifyDatabaseDDL($diff);
     $statements = PropelSQLParser::parseString($sql);
     foreach ($statements as $statement) {
         if (strpos($statement, 'DROP') === 0) {
             // drop statements cause errors since the table doesn't exist
             continue;
         }
         $stmt = $this->con->prepare($statement);
         if ($stmt instanceof PDOStatement) {
             // only execute if has no error
             $stmt->execute();
         }
     }
 }
Example #2
0
 /**
  * Generates Table classes
  * @return void
  */
 function generateModels($table_names = false)
 {
     if ($table_names === false) {
         $table_names = $this->getTableNames();
     } elseif (empty($table_names)) {
         return;
     }
     $options = $this->options;
     if (!is_dir($options['model_path']) && !mkdir($options['model_path'])) {
         throw new Exception('The directory ' . $options['model_path'] . ' does not exist.');
     }
     if (!is_dir($options['base_model_path']) && !mkdir($options['base_model_path'])) {
         throw new Exception('The directory ' . $options['base_model_path'] . ' does not exist.');
     }
     //Write php files for classes
     foreach ($table_names as &$table_name) {
         $class_name = $this->getModelName($table_name);
         $base_class = $this->getBaseModel($table_name);
         $base_file = "base{$class_name}.php";
         $base_file = $options['base_model_path'] . $base_file;
         if (!file_exists($base_file) || file_get_contents($base_file) != $base_class) {
             file_put_contents($base_file, $base_class);
         }
         $file = $options['model_path'] . $class_name . ".php";
         if (!file_exists($file)) {
             $class = $this->getModel($table_name);
             file_put_contents($file, $class);
             unset($class);
         }
     }
     $sql = $this->database->getPlatform()->getAddTablesDDL($this->database);
     // save XML to file
     file_put_contents($options['model_path'] . $this->getConnectionName() . "-schema.xml", $this->getSchema()->saveXML());
     // save SQL to file
     file_put_contents($options['model_path'] . $this->getConnectionName() . "-schema.sql", $sql);
 }
Example #3
0
 /**
  * Add a database to the list and sets the AppData property to this
  * AppData
  *
  * @param      db the database to add
  */
 public function addDatabase($db)
 {
     if ($db instanceof Database) {
         $db->setAppData($this);
         if ($db->getPlatform() === null) {
             $db->setPlatform($this->platform);
         }
         $this->dbList[] = $db;
         return $db;
     } else {
         // XML attributes array / hash
         $d = new Database();
         $d->setAppData($this);
         if ($d->getPlatform() === null) {
             $d->setPlatform($this->platform);
         }
         $d->loadFromXML($db);
         return $this->addDatabase($d);
         // calls self w/ different param type
     }
 }
 /**
  * Add a database to the list and sets the AppData property to this
  * AppData
  *
  * @param Database|string $db the database to add
  *
  * @return Database
  */
 public function addDatabase($db)
 {
     if ($db instanceof Database) {
         $db->setAppData($this);
         if ($db->getPlatform() === null) {
             if ($config = $this->getGeneratorConfig()) {
                 $pf = $config->getConfiguredPlatform(null, $db->getName());
                 $db->setPlatform($pf ? $pf : $this->platform);
             } else {
                 $db->setPlatform($this->platform);
             }
         }
         $this->dbList[] = $db;
         return $db;
     } else {
         // XML attributes array / hash
         $d = new Database();
         $d->setAppData($this);
         $d->loadFromXML($db);
         return $this->addDatabase($d);
         // calls self w/ different param type
     }
 }