RainLab\Builder\Classes\MigrationModel::save PHP Method

save() public method

Saves the migration and applies all outstanding migrations for the plugin.
public save ( $executeOnSave = true )
    public function save($executeOnSave = true)
    {
        $this->validate();
        if (!strlen($this->scriptFileName) || !$this->isNewModel()) {
            $this->assignFileName();
        }
        $originalFileContents = $this->saveScriptFile();
        try {
            $originalVersionData = $this->insertOrUpdateVersion();
        } catch (Exception $ex) {
            // Remove the script file, but don't rollback
            // the version.yaml.
            $this->rollbackSaving(null, $originalFileContents);
            throw $ex;
        }
        try {
            if ($executeOnSave) {
                VersionManager::instance()->updatePlugin($this->getPluginCodeObj()->toCode(), $this->version);
            }
        } catch (Exception $ex) {
            // Remove the script file, and rollback
            // the version.yaml.
            $this->rollbackSaving($originalVersionData, $originalFileContents);
            throw $ex;
        }
        $this->originalVersion = $this->version;
        $this->exists = true;
    }

Usage Example

 public function onDatabaseTableMigrationApply()
 {
     $pluginCode = new PluginCode(Request::input('plugin_code'));
     $model = new MigrationModel();
     $model->setPluginCodeObj($pluginCode);
     $model->fill($_POST);
     $operation = Input::get('operation');
     $table = Input::get('table');
     $model->scriptFileName = 'builder_table_' . $operation . '_' . $table;
     $model->makeScriptFileNameUnique();
     $codeGenerator = new TableMigrationCodeGenerator();
     $model->code = $codeGenerator->wrapMigrationCode($model->scriptFileName, $model->code, $pluginCode);
     try {
         $model->save();
     } catch (Exception $ex) {
         throw new ApplicationException($ex->getMessage());
     }
     $result = $this->controller->widget->databaseTabelList->updateList();
     $result = array_merge($result, $this->controller->widget->versionList->refreshActivePlugin());
     $result['builderResponseData'] = ['builderObjectName' => $table, 'tabId' => $this->getTabId($table), 'tabTitle' => $table, 'tableName' => $table, 'operation' => $operation, 'pluginCode' => $pluginCode->toCode()];
     return $result;
 }