Reputation: 2797
I so worried about my process right now I'm build many website and database and now I begin to make database by using PHP to create database and tables for my website. I think it may easy for configuration for me when I host this website I don't need to do or create database manual I just click run from my some of admin pages to run any method to generate database and then frond end will be work fine. But what I concern is the ability or stable or not Because I used a function and send SQL code to server without using Codeigniter Magration.
Here is my method Now this is a static method but I will update it but I want to know an cause that will happen in the future. So if all developer have any experienced please give me some idea because it is first times for me to build.
public function feadback()
{
$q = "DROP TABLE IF EXISTS `feadback`;";
$this->db->query($q);
$q = "CREATE TABLE IF NOT EXISTS `feadback` (
`fid` bigint(50) AUTO_INCREMENT,
`fk_c_id` bigint(50) NOT NULL,
`fk_group_id` bigint(50),
`fk_user_id` bigint(50),
`fk_product_id` bigint(50),
`feaddata` decimal(10,2) NOT NULL ,
`credit` decimal(10,2) NOT NULL ,
`b_debit` decimal(10,2) NOT NULL ,
`b_credit` decimal(10,2) NOT NULL ,
`description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`fid`),
KEY `fk_c_id` (`fk_c_id`),
KEY `fk_group_id` (`fk_group_id`),
KEY `fk_user_id` (`fk_user_id`),
KEY `fk_product_id` (`fk_product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=11 ;";
$this->db->query($q);
$foriengkey = "ALTER TABLE `feadback` ADD CONSTRAINT `feadback_ibfk_1` FOREIGN KEY (`fk_c_id`) REFERENCES `cat` (`c_id`) ON DELETE CASCADE ON UPDATE CASCADE;";
return $this->db->query($foriengkey);
}
Thank you very much for help
Upvotes: 0
Views: 50
Reputation: 645
modern php frameworks all have Migration feature. the key concept of Migration is migrare up/down which is create/drop table in your case.Your code works fine if you think there's no need to drop any table.
in development environment you upgrade a table and it works fine. but in production environment your php upgrade code may cause exception and make your database broken. but if you use Migration and you get an Exception in database upgrade, Migration will automatically rollback.
Upvotes: 1