/** * Syncs the model with the database schema. * * @param array The options array given to the table(s) to compile with. * @return Migration */ public function sync(array $options = array()) { // Loop through every table generated by the model. foreach ($this->_tables as $table) { // Search for any coresponding tables that exist in the database. $tables = $this->_db->list_tables($table->name); // List the key and value of the options array while (list($key, $value) = each($options)) { // Adding them all to the table $table->add_option($key, $value); } if (empty($tables)) { // If no coresponding tables were found, just create the table. $table->create(); } else { // Begin an alter statement. $alter = DB::alter($table->name); // List the columns in the coresponding table $columns = $this->_db->list_columns($table->name); // Loop through columns in our table foreach ($table->columns() as $name => $column) { // If there is a coresponding column if (isset($columns[$column->name])) { // Modify it. DB::alter($table->name)->modify($column)->execute($this->_db); } else { // If there isnt, then add it. $alter->add($column); } // We have a reference of the column in our model, so unset it. unset($columns[$name]); } // Loop through everything that wasn't unset foreach ($columns as $name => $column) { // And drop it, because we don't have it in our model. DB::alter($table->name)->drop($name, 'column')->execute($this->_db); } // Execute the alter query if we have columns to add if ($alter->compile($this->_db)) { $alter->execute($this->_db); } } } // Finally return the migration object. return $this; }
/** * Proxy method to Database list_columns. * * @return array */ public function list_columns() { // Proxy to database return $this->_db->list_columns($this->_table_name); }