Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhaupin/7b5bfb48678a30fc4174ca955881d2d4 to your computer and use it in GitHub Desktop.
Save dhaupin/7b5bfb48678a30fc4174ca955881d2d4 to your computer and use it in GitHub Desktop.
Snippet - Table field install/uninstall functions
<?php
// These 3 functions are part of a more extensive module class
private function chkField($table, $field) {
$field = $this->db->query("DESC " . DB_PREFIX . $table . " '%" . $field . "%'");
if ($field->row) {
return true;
}
return false;
}
public function install() {
if (!$this->chkField('some_existing_table', 'a_new_field')) {
$this->db->query("ALTER TABLE some_existing_table ADD a_new_field VARCHAR(128) NOT NULL AFTER some_existing_field;");
}
}
public function uninstall() {
if ($this->config->get('uninstall_remove_data')) {
if ($this->chkField('some_existing_table', 'a_new_field')) {
$this->db->query("ALTER TABLE some_existing_table DROP a_new_field;");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment