· 8 years ago · Feb 28, 2018, 07:52 PM
1 public function createTable_addressMapping()
2 {
3 $this->load->dbforge();
4 $table_name = 'product_custom_field_mapping';
5
6 // test if table exists
7 if (! $this->db->table_exists($table_name))
8 {
9 echo '<p>Table ' . $table_name . ' does not exist. Creating table...</p>';
10
11 // create the table
12 $fields = array(
13 'id' => array(
14 'type' => 'INT',
15 'auto_increment' => true
16 ),
17
18 'company_id' => array(
19 'type' => 'INT'
20 ),
21
22 'product_custom_field_id' => array(
23 'type' => 'INT'
24 ),
25
26 'facilities_id' => array(
27 'type' => 'INT'
28 ),
29
30 'facilities_field_name' => array(
31 'type' => 'VARCHAR',
32 'constraint' => '255',
33 'null' => FALSE
34 ),
35
36 'date_created' => array(
37 'type' => 'datetime'
38 ),
39
40 'date_modified' => array(
41 'type' => 'datetime'
42 )
43 );
44
45 // Add the fields
46 try
47 {
48 $this->dbforge->add_field($fields);
49 }
50 catch(Exception $e)
51 {
52 echo '<p>Faild to add fields</p>';
53 echo $e->getMesage();
54 exit;
55 }
56
57 // set primary key
58 try
59 {
60 $this->dbforge->add_key('id', TRUE);
61 }
62 catch(Exception $e)
63 {
64 echo '<p>Faild to set primary key</p>';
65 echo $e->getMesage();
66 exit;
67 }
68
69 // set index on company_id
70 try
71 {
72 $this->dbforge->add_key('company_id');
73 }
74 catch(Exception $e)
75 {
76 echo '<p>Faild to set index on company_id</p>';
77 echo $e->getMessage();
78 exit;
79 }
80
81 // set index on product_custom_field_id
82 try
83 {
84 $this->dbforge->add_key('product_custom_field_id');
85 }
86 catch(Exception $e)
87 {
88 echo '<p>Faild to set index on product_custom_field_id</p>';
89 echo $e->getMessage();
90 exit;
91 }
92
93 // set index on date_created
94 try
95 {
96 $this->dbforge->add_key('date_created');
97 }
98 catch(Exception $e)
99 {
100 echo '<p>Faild to set index on date_created</p>';
101 echo $e->getMessage();
102 exit;
103 }
104
105 // create the table
106 try
107 {
108 $this->dbforge->create_table($table_name, TRUE);
109 }
110 catch(Exception $e)
111 {
112 echo '<p>There was a problem creating the table</p>';
113 echo $e->getMessage();
114 exit;
115 }
116
117 echo '<p>Table created.</p>';
118
119 }
120 else
121 {
122 echo '<p>Table already exists.</p>';
123 }
124
125
126 }