Last active
August 18, 2023 03:15
-
-
Save natanfelles/4024b598f3b31db47c3e139d82dec281 to your computer and use it in GitHub Desktop.
CodeIgniter Database Helper to Add and Drop Foreign Keys and Triggers
This file contains 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 | |
/** | |
* @author Natan Felles <natanfelles@gmail.com> | |
*/ | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
if ( ! function_exists('add_foreign_key')) | |
{ | |
/** | |
* @param string $table Table name | |
* @param string $foreign_key Collumn name having the Foreign Key | |
* @param string $references Table and column reference. Ex: users(id) | |
* @param string $on_delete RESTRICT, NO ACTION, CASCADE, SET NULL, SET DEFAULT | |
* @param string $on_update RESTRICT, NO ACTION, CASCADE, SET NULL, SET DEFAULT | |
* | |
* @return string SQL command | |
*/ | |
function add_foreign_key($table, $foreign_key, $references, $on_delete = 'RESTRICT', $on_update = 'RESTRICT') | |
{ | |
$references = explode('(', str_replace(')', '', str_replace('`', '', $references))); | |
return "ALTER TABLE `{$table}` ADD CONSTRAINT `{$table}_{$foreign_key}_fk` FOREIGN KEY (`{$foreign_key}`) REFERENCES `{$references[0]}`(`{$references[1]}`) ON DELETE {$on_delete} ON UPDATE {$on_update}"; | |
} | |
} | |
if ( ! function_exists('drop_foreign_key')) | |
{ | |
/** | |
* @param string $table Table name | |
* @param string $foreign_key Collumn name having the Foreign Key | |
* | |
* @return string SQL command | |
*/ | |
function drop_foreign_key($table, $foreign_key) | |
{ | |
return "ALTER TABLE `{$table}` DROP FOREIGN KEY `{$table}_{$foreign_key}_fk`"; | |
} | |
} | |
if ( ! function_exists('add_trigger')) | |
{ | |
/** | |
* @param string $trigger_name Trigger name | |
* @param string $table Table name | |
* @param string $statement Command to run | |
* @param string $time BEFORE or AFTER | |
* @param string $event INSERT, UPDATE or DELETE | |
* @param string $type FOR EACH ROW [FOLLOWS|PRECEDES] | |
* | |
* @return string SQL Command | |
*/ | |
function add_trigger($trigger_name, $table, $statement, $time = 'BEFORE', $event = 'INSERT', $type = 'FOR EACH ROW') | |
{ | |
return 'DELIMITER ;;' . PHP_EOL . "CREATE TRIGGER `{$trigger_name}` {$time} {$event} ON `{$table}` {$type}" . PHP_EOL . 'BEGIN' . PHP_EOL . $statement . PHP_EOL . 'END;' . PHP_EOL . 'DELIMITER ;;'; | |
} | |
} | |
if ( ! function_exists('drop_trigger')) | |
{ | |
/** | |
* @param string $trigger_name Trigger name | |
* | |
* @return string SQL Command | |
*/ | |
function drop_trigger($trigger_name) | |
{ | |
return "DROP TRIGGER `{$trigger_name}`;"; | |
} | |
} |
@andikaryanto application/helpers
https://natanfelles.github.io/blog/codeigniter-db_helper
+2 functions
if(!function_exists('drop_function')){
/**
* @param string $function_name Function name
*
* @return string SQL Command
*/
function drop_function($function_name)
{
return "DROP FUNCTION `{$function_name}`;";
}
}
if(!function_exists('drop_view')){
/**
* @param string $view_name View name
*
* @return string SQL Command
*/
function drop_view($view_name)
{
return "DROP VIEW `{$view_name}`;";
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where should i place this file?