diff --git a/README.markdown b/README.markdown index 025d354..d2359cc 100644 --- a/README.markdown +++ b/README.markdown @@ -147,6 +147,9 @@ rename_column($table_name, $column_name, $new_column_name) change_column($table_name, $column_name, $params) remove_column($table_name, $column_name) +add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name) +remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name) + add_index($table_name, $index_name, $columns, $index_type = 'normal') remove_index($table_name, $index_name) ``` diff --git a/classes/Drivers/Driver.php b/classes/Drivers/Driver.php index 0ea0561..6eab497 100644 --- a/classes/Drivers/Driver.php +++ b/classes/Drivers/Driver.php @@ -134,6 +134,32 @@ abstract public function change_column($table_name, $column_name, $params); */ abstract public function remove_column($table_name, $column_name); + /** + * Add a foreign key to a table + * + * @example add_foreign_key ( "client", "city_id", "city", "id"); + * + * @param string Name of the table + * @param string Name of the column + * @param string Name of referenced table + * @param string Name of referenced column + * @return boll Returns true if no errors + */ + abstract public function add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name); + + /** + * Remove a foreign key from a table + * + * @example remove_foreign_key ( "client", "city_id", "city", "id"); + * + * @param string Name of the table + * @param string Name of the column + * @param string Name of referenced table + * @param string Name of referenced column + * @return boll Returns true if no errors + */ + abstract public function remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name); + /** * Add an index * diff --git a/classes/Drivers/Mysql.php b/classes/Drivers/Mysql.php index 0f1d8a1..e6852cd 100644 --- a/classes/Drivers/Mysql.php +++ b/classes/Drivers/Mysql.php @@ -91,6 +91,18 @@ public function remove_column($table_name, $column_name) return $this->run_query("ALTER TABLE $table_name DROP COLUMN $column_name ;"); } + public function add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name) + { + $fk = 'fk'.$table_name.$column_name.$referenced_table_name.$referenced_column_name; + return $this->run_query("ALTER TABLE $table_name ADD CONSTRAINT $fk " + ."FOREIGN KEY ( `$column_name` ) REFERENCES `$referenced_table_name` ( `$referenced_column_name` ); "); + } + + public function remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name){ + $fk = 'fk'.$table_name.$column_name.$referenced_table_name.$referenced_column_name; + return $this->run_query("ALTER TABLE $table_name DROP FOREIGN KEY $fk;"); + } + public function add_index($table_name, $index_name, $columns, $index_type = 'normal') { switch ($index_type) diff --git a/classes/Migration.php b/classes/Migration.php index 4bc37e6..b6c6d10 100644 --- a/classes/Migration.php +++ b/classes/Migration.php @@ -199,6 +199,40 @@ public function remove_column($table_name, $column_name) return $ret; } + /** + * Add a foreign key to a table + * + * @example add_foreign_key ( "client", "city_id", "city", "id"); + * + * @param string Name of the table + * @param string Name of the column + * @param string Name of referenced table + * @param string Name of referenced column + * @return boll Returns true if no errors + */ + public function add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name) + { + $ret = $this->driver->add_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name); + return $ret; + } + + /** + * Add a foreign key to a table + * + * @example remove_foreign_key ( "client", "city_id", "city", "id"); + * + * @param string Name of the table + * @param string Name of the column + * @param string Name of referenced table + * @param string Name of referenced column + * @return boll Returns true if no errors + */ + public function remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name) + { + $ret = $this->driver->remove_foreign_key($table_name, $column_name, $referenced_table_name, $referenced_column_name); + return $ret; + } + /** * Add an index *