Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | #################### |
| 2 | Database Forge Class |
| 3 | #################### |
| 4 | |
| 5 | The Database Forge Class contains functions that help you manage your |
| 6 | database. |
| 7 | |
| 8 | .. contents:: Table of Contents |
| 9 | |
| 10 | **************************** |
| 11 | Initializing the Forge Class |
| 12 | **************************** |
| 13 | |
| 14 | .. important:: In order to initialize the Forge class, your database |
| 15 | driver must already be running, since the forge class relies on it. |
| 16 | |
| 17 | Load the Forge Class as follows:: |
| 18 | |
| 19 | $this->load->dbforge() |
| 20 | |
| 21 | Once initialized you will access the functions using the $this->dbforge |
| 22 | object:: |
| 23 | |
| 24 | $this->dbforge->some_function() |
| 25 | |
| 26 | $this->dbforge->create_database('db_name') |
| 27 | ============================================ |
| 28 | |
| 29 | Permits you to create the database specified in the first parameter. |
| 30 | Returns TRUE/FALSE based on success or failure:: |
| 31 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 32 | if ($this->dbforge->create_database('my_db')) |
| 33 | { |
| 34 | echo 'Database created!'; |
| 35 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | |
| 37 | $this->dbforge->drop_database('db_name') |
| 38 | ========================================== |
| 39 | |
| 40 | Permits you to drop the database specified in the first parameter. |
| 41 | Returns TRUE/FALSE based on success or failure:: |
| 42 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 43 | if ($this->dbforge->drop_database('my_db')) |
| 44 | { |
| 45 | echo 'Database deleted!'; |
| 46 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
| 48 | **************************** |
| 49 | Creating and Dropping Tables |
| 50 | **************************** |
| 51 | |
| 52 | There are several things you may wish to do when creating tables. Add |
| 53 | fields, add keys to the table, alter columns. CodeIgniter provides a |
| 54 | mechanism for this. |
| 55 | |
| 56 | Adding fields |
| 57 | ============= |
| 58 | |
| 59 | Fields are created via an associative array. Within the array you must |
| 60 | include a 'type' key that relates to the datatype of the field. For |
| 61 | example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR) |
| 62 | also require a 'constraint' key. |
| 63 | |
| 64 | :: |
| 65 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 66 | $fields = array( |
| 67 | 'users' => array( |
| 68 | 'type' => 'VARCHAR', |
| 69 | 'constraint' => '100', |
| 70 | ), |
| 71 | ); |
| 72 | // will translate to "users VARCHAR(100)" when the field is added. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | |
| 74 | |
| 75 | Additionally, the following key/values can be used: |
| 76 | |
| 77 | - unsigned/true : to generate "UNSIGNED" in the field definition. |
| 78 | - default/value : to generate a default value in the field definition. |
| 79 | - null/true : to generate "NULL" in the field definition. Without this, |
| 80 | the field will default to "NOT NULL". |
| 81 | - auto_increment/true : generates an auto_increment flag on the |
| 82 | field. Note that the field type must be a type that supports this, |
| 83 | such as integer. |
| 84 | |
| 85 | :: |
| 86 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 87 | $fields = array( |
| 88 | 'blog_id' => array( |
| 89 | 'type' => 'INT', |
| 90 | 'constraint' => 5, |
| 91 | 'unsigned' => TRUE, |
| 92 | 'auto_increment' => TRUE |
| 93 | ), |
| 94 | 'blog_title' => array( |
| 95 | 'type' => 'VARCHAR', |
| 96 | 'constraint' => '100', |
| 97 | ), |
| 98 | 'blog_author' => array( |
| 99 | 'type' =>'VARCHAR', |
| 100 | 'constraint' => '100', |
| 101 | 'default' => 'King of Town', |
| 102 | ), |
| 103 | 'blog_description' => array( |
| 104 | 'type' => 'TEXT', |
| 105 | 'null' => TRUE, |
| 106 | ), |
| 107 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
| 109 | |
| 110 | After the fields have been defined, they can be added using |
| 111 | $this->dbforge->add_field($fields); followed by a call to the |
| 112 | create_table() function. |
| 113 | |
| 114 | $this->dbforge->add_field() |
| 115 | ---------------------------- |
| 116 | |
| 117 | The add fields function will accept the above array. |
| 118 | |
| 119 | Passing strings as fields |
| 120 | ------------------------- |
| 121 | |
| 122 | If you know exactly how you want a field to be created, you can pass the |
| 123 | string into the field definitions with add_field() |
| 124 | |
| 125 | :: |
| 126 | |
| 127 | $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'"); |
| 128 | |
| 129 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 130 | .. note:: Multiple calls to add_field() are cumulative. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
| 132 | Creating an id field |
| 133 | -------------------- |
| 134 | |
| 135 | There is a special exception for creating id fields. A field with type |
| 136 | id will automatically be assinged as an INT(9) auto_incrementing |
| 137 | Primary Key. |
| 138 | |
| 139 | :: |
| 140 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 141 | $this->dbforge->add_field('id'); |
| 142 | // gives id INT(9) NOT NULL AUTO_INCREMENT |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
| 144 | |
| 145 | Adding Keys |
| 146 | =========== |
| 147 | |
| 148 | Generally speaking, you'll want your table to have Keys. This is |
| 149 | accomplished with $this->dbforge->add_key('field'). An optional second |
| 150 | parameter set to TRUE will make it a primary key. Note that add_key() |
| 151 | must be followed by a call to create_table(). |
| 152 | |
| 153 | Multiple column non-primary keys must be sent as an array. Sample output |
| 154 | below is for MySQL. |
| 155 | |
| 156 | :: |
| 157 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 158 | $this->dbforge->add_key('blog_id', TRUE); |
| 159 | // gives PRIMARY KEY `blog_id` (`blog_id`) |
| 160 | |
| 161 | $this->dbforge->add_key('blog_id', TRUE); |
| 162 | $this->dbforge->add_key('site_id', TRUE); |
| 163 | // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`) |
| 164 | |
| 165 | $this->dbforge->add_key('blog_name'); |
| 166 | // gives KEY `blog_name` (`blog_name`) |
| 167 | |
| 168 | $this->dbforge->add_key(array('blog_name', 'blog_label')); |
| 169 | // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
| 171 | |
| 172 | Creating a table |
| 173 | ================ |
| 174 | |
| 175 | After fields and keys have been declared, you can create a new table |
| 176 | with |
| 177 | |
| 178 | :: |
| 179 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 180 | $this->dbforge->create_table('table_name'); |
| 181 | // gives CREATE TABLE table_name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 182 | |
| 183 | |
| 184 | An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause |
| 185 | into the definition |
| 186 | |
| 187 | :: |
| 188 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 189 | $this->dbforge->create_table('table_name', TRUE); |
| 190 | // gives CREATE TABLE IF NOT EXISTS table_name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 191 | |
| 192 | |
| 193 | Dropping a table |
| 194 | ================ |
| 195 | |
| 196 | Executes a DROP TABLE sql |
| 197 | |
| 198 | :: |
| 199 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 200 | $this->dbforge->drop_table('table_name'); |
| 201 | // gives DROP TABLE IF EXISTS table_name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 202 | |
| 203 | |
| 204 | Renaming a table |
| 205 | ================ |
| 206 | |
| 207 | Executes a TABLE rename |
| 208 | |
| 209 | :: |
| 210 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 211 | $this->dbforge->rename_table('old_table_name', 'new_table_name'); |
| 212 | // gives ALTER TABLE old_table_name RENAME TO new_table_name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 213 | |
| 214 | |
| 215 | **************** |
| 216 | Modifying Tables |
| 217 | **************** |
| 218 | |
| 219 | $this->dbforge->add_column() |
| 220 | ============================= |
| 221 | |
| 222 | The add_column() function is used to modify an existing table. It |
| 223 | accepts the same field array as above, and can be used for an unlimited |
| 224 | number of additional fields. |
| 225 | |
| 226 | :: |
| 227 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 228 | $fields = array( |
| 229 | 'preferences' => array('type' => 'TEXT') |
| 230 | ); |
| 231 | $this->dbforge->add_column('table_name', $fields); |
| 232 | // gives ALTER TABLE table_name ADD preferences TEXT |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 233 | |
| 234 | An optional third parameter can be used to specify which existing column |
| 235 | to add the new column after. |
| 236 | |
| 237 | :: |
| 238 | |
| 239 | $this->dbforge->add_column('table_name', $fields, 'after_field'); |
| 240 | |
| 241 | |
| 242 | $this->dbforge->drop_column() |
| 243 | ============================== |
| 244 | |
| 245 | Used to remove a column from a table. |
| 246 | |
| 247 | :: |
| 248 | |
| 249 | $this->dbforge->drop_column('table_name', 'column_to_drop'); |
| 250 | |
| 251 | |
| 252 | $this->dbforge->modify_column() |
| 253 | ================================ |
| 254 | |
| 255 | The usage of this function is identical to add_column(), except it |
| 256 | alters an existing column rather than adding a new one. In order to |
| 257 | change the name you can add a "name" key into the field defining array. |
| 258 | |
| 259 | :: |
| 260 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 261 | $fields = array( |
| 262 | 'old_name' => array( |
| 263 | 'name' => 'new_name', |
| 264 | 'type' => 'TEXT', |
| 265 | ), |
| 266 | ); |
| 267 | $this->dbforge->modify_column('table_name', $fields); |
| 268 | // gives ALTER TABLE table_name CHANGE old_name new_name TEXT |