/** * Save a Field to the database * * @param Field|Saveable $item The field to save * @return bool True on success, false on failure * @throws WireException * */ public function ___save(Saveable $item) { if ($item->flags & Field::flagFieldgroupContext) { throw new WireException("Field {$item} is not saveable because it is in a specific context"); } if (!strlen($item->name)) { throw new WireException("Field name is required"); } $database = $this->wire('database'); $isNew = $item->id < 1; $prevTable = $database->escapeTable($item->prevTable); $table = $database->escapeTable($item->getTable()); if (!$isNew && $prevTable && $prevTable != $table) { // note that we rename the table twice in order to force MySQL to perform the rename // even if only the case has changed. $database->exec("RENAME TABLE `{$prevTable}` TO `tmp_{$table}`"); // QA $database->exec("RENAME TABLE `tmp_{$table}` TO `{$table}`"); // QA $item->prevTable = ''; } if ($item->prevFieldtype && $item->prevFieldtype->name != $item->type->name) { if (!$this->changeFieldtype($item)) { $item->type = $item->prevFieldtype; $this->error("Error changing fieldtype for '{$item}', reverted back to '{$item->type}'"); } else { $item->prevFieldtype = null; } } if (!$item->type) { throw new WireException("Can't save a Field that doesn't have it's 'type' property set to a Fieldtype"); } if (!parent::___save($item)) { return false; } if ($isNew) { $item->type->createField($item); } if ($item->flags & Field::flagGlobal) { // make sure that all template fieldgroups contain this field and add to any that don't. foreach (wire('templates') as $template) { if ($template->noGlobal) { continue; } $fieldgroup = $template->fieldgroup; if (!$fieldgroup->hasField($item)) { $fieldgroup->add($item); $fieldgroup->save(); if (wire('config')->debug) { $this->message("Added field '{$item->name}' to template/fieldgroup '{$fieldgroup->name}'"); } } } } return true; }
/** * Per WireSaveableItems interface, save a Field to the database * */ public function ___save(Saveable $item) { if ($item->prevTable && strtolower($item->prevTable) != strtolower($item->getTable())) { $this->fuel('db')->query("RENAME TABLE {$item->prevTable} TO " . $item->getTable()); $item->prevTable = ''; } if ($item->prevFieldtype && $item->prevFieldtype->name != $item->type->name) { if (!$this->changeFieldtype($item)) { $item->type = $item->prevFieldtype; $this->error("Error changing fieldtype for '{$item}', reverted back to '{$item->type}'"); } else { $item->prevFieldtype = null; } } $isNew = !$item->id; if (!$item->type) { throw new WireException("Can't save a Field that doesn't have it's 'type' property set to a Fieldtype"); } if (!parent::___save($item)) { return false; } if ($isNew) { $item->type->createField($item); } return true; }