Created
April 13, 2017 18:00
-
-
Save dhaupin/7b5bfb48678a30fc4174ca955881d2d4 to your computer and use it in GitHub Desktop.
Snippet - Table field install/uninstall functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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