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