dbforge->add_field([ 'id' => array('type' => 'INT','constraint' => 11,'unsigned' => TRUE,'auto_increment' => TRUE), 'name' => array('type' => 'VARCHAR', 'constraint' => '255'), 'isbn' => array('type' => 'VARCHAR', 'constraint' => '255'), 'status' => array('type' => 'INT', 'constraint' => 11, 'null' => TRUE), 'created_at' => array('type' => 'DATE', 'null' => TRUE), 'updated_at' => array('type' => 'DATETIME', 'null' => TRUE)]); $this->dbforge->add_key('id', TRUE); $this->dbforge->create_table('textbook'); $this->seed_data(); } public function down() { $this->dbforge->drop_table('textbook'); } public function seed_data() { $data = [ [ 'name' => 'Head First Java', 'isbn' => 'ISBN_1234567', 'status' => 1, 'created_at' => date('Y-m-j'), 'updated_at' => date('Y-m-j H:i:s'), ], [ 'name' => 'Oxford Practice Grammar', 'isbn' => 'ISBN_4569821', 'status' => 1, 'created_at' => date('Y-m-j'), 'updated_at' => date('Y-m-j H:i:s'), ], [ 'name' => 'Algorithms', 'isbn' => 'ISBN_9876545', 'status' => 1, 'created_at' => date('Y-m-j'), 'updated_at' => date('Y-m-j H:i:s'), ], ]; foreach ($data as $k => $seed ) { foreach ($seed as $key => $value) { $seed[$key] = '\'' . addslashes($value) . '\''; } $row = array_values($seed); array_unshift($row, (string)($k + 1)); $sql = 'INSERT INTO textbook VALUES ' . '(' . implode(',', $row) . ')'; error_log($sql); $this->db->query($sql); } } }