/** * Handle the update of a MetaModel and all attached data. * * @param PostPersistModelEvent $event The event. * * @return void */ public function handleUpdate(PostPersistModelEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel') { return; } $old = $event->getOriginalModel(); $new = $event->getModel(); $oldTable = $old ? $old->getProperty('tableName') : null; $newTable = $new->getProperty('tableName'); // Table name changed? if ($oldTable !== $newTable) { if ($oldTable && $this->getDatabase()->tableExists($oldTable, null, true)) { TableManipulation::renameTable($oldTable, $newTable); // TODO: notify attributes that the MetaModel has changed its table name. } else { TableManipulation::createTable($newTable); } } TableManipulation::setVariantSupport($newTable, $new->getProperty('varsupport')); }
/** * Encode the given value from a real language array into a serialized language array. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void * * @throws \RuntimeException When the column name is illegal or duplicate. */ public function encodeColumnNameValue(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_attribute' || $event->getProperty() !== 'colname') { return; } $oldColumnName = $event->getModel()->getProperty($event->getProperty()); $columnName = $event->getValue(); $metaModel = $this->getMetaModelByModelPid($event->getModel()); if (!$columnName || $oldColumnName !== $columnName) { TableManipulation::checkColumnDoesNotExist($metaModel->getTableName(), $columnName); $colNames = array_keys($metaModel->getAttributes()); if (in_array($columnName, $colNames)) { throw new \RuntimeException(sprintf($event->getEnvironment()->getTranslator()->translate('columnExists', 'ERR'), $columnName, $metaModel->getTableName())); } } }
/** * Renames the underlying database structure for this field. * * @param string $strNewColumnName The new column name. * * @return void */ public function renameColumn($strNewColumnName) { TableManipulation::checkColumnName($strNewColumnName); if ($this->getColName() && $this->getMetaModel()->getServiceContainer()->getDatabase()->fieldExists($this->getColName(), $this->getMetaModel()->getTableName(), true)) { TableManipulation::renameColumn($this->getMetaModel()->getTableName(), $this->getColName(), $strNewColumnName, $this->getSQLDataType()); } else { $strBackupColName = $this->getColName(); $this->set('colname', $strNewColumnName); $this->createColumn(); $this->set('colname', $strBackupColName); } }