Skip to content

Instantly share code, notes, and snippets.

@natanfelles
Last active August 18, 2023 03:15
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save natanfelles/4024b598f3b31db47c3e139d82dec281 to your computer and use it in GitHub Desktop.
Save natanfelles/4024b598f3b31db47c3e139d82dec281 to your computer and use it in GitHub Desktop.
CodeIgniter Database Helper to Add and Drop Foreign Keys and Triggers
<?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}`;";
}
}
@natanfelles
Copy link
Author

Must be used in a query, like:

$fields = array(
	'id'         => [
		'type'           => 'INT(11)',
		'auto_increment' => TRUE,
	],
	'user_id'    => [
		'type'     => 'INT(11)',
	],
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table($this->table);
$this->db->query(add_foreign_key($this->table, 'user_id', 'users(id)', 'CASCADE', 'CASCADE'));

and

$this->db->query(drop_foreign_key($this->table, 'user_id'));
$this->dbforge->drop_table($this->table);

@andikaryanto
Copy link

where should i place this file?

@natanfelles
Copy link
Author

@d3ividdy
Copy link

+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