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 |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 9 | :depth: 3 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 10 | |
| 11 | **************************** |
| 12 | Initializing the Forge Class |
| 13 | **************************** |
| 14 | |
| 15 | .. important:: In order to initialize the Forge class, your database |
| 16 | driver must already be running, since the forge class relies on it. |
| 17 | |
| 18 | Load the Forge Class as follows:: |
| 19 | |
| 20 | $this->load->dbforge() |
| 21 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 22 | You can also pass another database object to the DB Forge loader, in case |
| 23 | the database you want to manage isn't the default one:: |
| 24 | |
| 25 | $this->myforge = $this->load->dbforge($this->other_db, TRUE); |
| 26 | |
| 27 | In the above example, we're passing a custom database object as the first |
| 28 | parameter and then tell it to return the dbforge object, instead of |
| 29 | assigning it directly to ``$this->dbforge``. |
| 30 | |
| 31 | .. note:: Both of the parameters can be used individually, just pass an empty |
| 32 | value as the first one if you wish to skip it. |
| 33 | |
| 34 | Once initialized you will access the methods using the ``$this->dbforge`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | object:: |
| 36 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 37 | $this->dbforge->some_method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 39 | ******************************* |
| 40 | Creating and Dropping Databases |
| 41 | ******************************* |
| 42 | |
| 43 | **$this->dbforge->create_database('db_name')** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
| 45 | Permits you to create the database specified in the first parameter. |
| 46 | Returns TRUE/FALSE based on success or failure:: |
| 47 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 48 | if ($this->dbforge->create_database('my_db')) |
| 49 | { |
| 50 | echo 'Database created!'; |
| 51 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 53 | **$this->dbforge->drop_database('db_name')** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
| 55 | Permits you to drop the database specified in the first parameter. |
| 56 | Returns TRUE/FALSE based on success or failure:: |
| 57 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 58 | if ($this->dbforge->drop_database('my_db')) |
| 59 | { |
| 60 | echo 'Database deleted!'; |
| 61 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 63 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | **************************** |
| 65 | Creating and Dropping Tables |
| 66 | **************************** |
| 67 | |
| 68 | There are several things you may wish to do when creating tables. Add |
| 69 | fields, add keys to the table, alter columns. CodeIgniter provides a |
| 70 | mechanism for this. |
| 71 | |
| 72 | Adding fields |
| 73 | ============= |
| 74 | |
| 75 | Fields are created via an associative array. Within the array you must |
| 76 | include a 'type' key that relates to the datatype of the field. For |
| 77 | example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR) |
| 78 | also require a 'constraint' key. |
| 79 | |
| 80 | :: |
| 81 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 82 | $fields = array( |
| 83 | 'users' => array( |
| 84 | 'type' => 'VARCHAR', |
| 85 | 'constraint' => '100', |
| 86 | ), |
| 87 | ); |
| 88 | // will translate to "users VARCHAR(100)" when the field is added. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 89 | |
| 90 | |
| 91 | Additionally, the following key/values can be used: |
| 92 | |
| 93 | - unsigned/true : to generate "UNSIGNED" in the field definition. |
| 94 | - default/value : to generate a default value in the field definition. |
| 95 | - null/true : to generate "NULL" in the field definition. Without this, |
| 96 | the field will default to "NOT NULL". |
| 97 | - auto_increment/true : generates an auto_increment flag on the |
| 98 | field. Note that the field type must be a type that supports this, |
| 99 | such as integer. |
| 100 | |
| 101 | :: |
| 102 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 103 | $fields = array( |
| 104 | 'blog_id' => array( |
| 105 | 'type' => 'INT', |
| 106 | 'constraint' => 5, |
| 107 | 'unsigned' => TRUE, |
| 108 | 'auto_increment' => TRUE |
| 109 | ), |
| 110 | 'blog_title' => array( |
| 111 | 'type' => 'VARCHAR', |
| 112 | 'constraint' => '100', |
| 113 | ), |
| 114 | 'blog_author' => array( |
| 115 | 'type' =>'VARCHAR', |
| 116 | 'constraint' => '100', |
| 117 | 'default' => 'King of Town', |
| 118 | ), |
| 119 | 'blog_description' => array( |
| 120 | 'type' => 'TEXT', |
| 121 | 'null' => TRUE, |
| 122 | ), |
| 123 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
| 125 | |
| 126 | After the fields have been defined, they can be added using |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 127 | ``$this->dbforge->add_field($fields);`` followed by a call to the |
| 128 | ``create_table()`` method. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 129 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 130 | **$this->dbforge->add_field()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 132 | The add fields method will accept the above array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 134 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 135 | Passing strings as fields |
| 136 | ------------------------- |
| 137 | |
| 138 | If you know exactly how you want a field to be created, you can pass the |
| 139 | string into the field definitions with add_field() |
| 140 | |
| 141 | :: |
| 142 | |
| 143 | $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'"); |
| 144 | |
| 145 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 146 | .. note:: Multiple calls to add_field() are cumulative. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
| 148 | Creating an id field |
| 149 | -------------------- |
| 150 | |
| 151 | There is a special exception for creating id fields. A field with type |
| 152 | id will automatically be assinged as an INT(9) auto_incrementing |
| 153 | Primary Key. |
| 154 | |
| 155 | :: |
| 156 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 157 | $this->dbforge->add_field('id'); |
| 158 | // gives id INT(9) NOT NULL AUTO_INCREMENT |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | |
| 160 | |
| 161 | Adding Keys |
| 162 | =========== |
| 163 | |
| 164 | Generally speaking, you'll want your table to have Keys. This is |
| 165 | accomplished with $this->dbforge->add_key('field'). An optional second |
| 166 | parameter set to TRUE will make it a primary key. Note that add_key() |
| 167 | must be followed by a call to create_table(). |
| 168 | |
| 169 | Multiple column non-primary keys must be sent as an array. Sample output |
| 170 | below is for MySQL. |
| 171 | |
| 172 | :: |
| 173 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 174 | $this->dbforge->add_key('blog_id', TRUE); |
| 175 | // gives PRIMARY KEY `blog_id` (`blog_id`) |
| 176 | |
| 177 | $this->dbforge->add_key('blog_id', TRUE); |
| 178 | $this->dbforge->add_key('site_id', TRUE); |
| 179 | // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`) |
| 180 | |
| 181 | $this->dbforge->add_key('blog_name'); |
| 182 | // gives KEY `blog_name` (`blog_name`) |
| 183 | |
| 184 | $this->dbforge->add_key(array('blog_name', 'blog_label')); |
| 185 | // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 186 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 187 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 188 | |
| 189 | Creating a table |
| 190 | ================ |
| 191 | |
| 192 | After fields and keys have been declared, you can create a new table |
| 193 | with |
| 194 | |
| 195 | :: |
| 196 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 197 | $this->dbforge->create_table('table_name'); |
| 198 | // gives CREATE TABLE table_name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 199 | |
| 200 | |
| 201 | An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause |
| 202 | into the definition |
| 203 | |
| 204 | :: |
| 205 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 206 | $this->dbforge->create_table('table_name', TRUE); |
| 207 | // gives CREATE TABLE IF NOT EXISTS table_name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 208 | |
Andrey Andreev | 27f798b | 2014-01-20 18:19:13 +0200 | [diff] [blame] | 209 | You could also pass optional table attributes, such as MySQL's ``ENGINE``:: |
| 210 | |
| 211 | $attributes = array('ENGINE' => 'InnoDB'); |
| 212 | $this->dbforge->create_table('table_name', FALSE, $attributes); |
| 213 | // produces: CREATE TABLE `table_name` (...) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci |
| 214 | |
| 215 | .. note:: Unless you specify the ``CHARACTER SET`` and/or ``COLLATE`` attributes, |
| 216 | ``create_table()`` will always add them with your configured *char_set* |
| 217 | and *dbcollat* values, as long as they are not empty (MySQL only). |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 218 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 219 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 220 | Dropping a table |
| 221 | ================ |
| 222 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 223 | Execute a DROP TABLE statement and optionally add an IF EXISTS clause. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 224 | |
| 225 | :: |
| 226 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 227 | // Produces: DROP TABLE table_name |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 228 | $this->dbforge->drop_table('table_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 229 | |
Andrey Andreev | a287a34 | 2012-11-05 23:19:59 +0200 | [diff] [blame] | 230 | // Produces: DROP TABLE IF EXISTS table_name |
| 231 | $this->dbforge->drop_table('table_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 232 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 233 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 234 | Renaming a table |
| 235 | ================ |
| 236 | |
| 237 | Executes a TABLE rename |
| 238 | |
| 239 | :: |
| 240 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 241 | $this->dbforge->rename_table('old_table_name', 'new_table_name'); |
| 242 | // gives ALTER TABLE old_table_name RENAME TO new_table_name |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 243 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 244 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 245 | |
| 246 | **************** |
| 247 | Modifying Tables |
| 248 | **************** |
| 249 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 250 | Adding a Column to a Table |
| 251 | ========================== |
| 252 | |
| 253 | **$this->dbforge->add_column()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 254 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 255 | The ``add_column()`` method is used to modify an existing table. It |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 256 | accepts the same field array as above, and can be used for an unlimited |
| 257 | number of additional fields. |
| 258 | |
| 259 | :: |
| 260 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 261 | $fields = array( |
| 262 | 'preferences' => array('type' => 'TEXT') |
| 263 | ); |
| 264 | $this->dbforge->add_column('table_name', $fields); |
Andrey Andreev | b67277b | 2012-11-12 12:51:14 +0200 | [diff] [blame] | 265 | // Executes: ALTER TABLE table_name ADD preferences TEXT |
| 266 | |
| 267 | If you are using MySQL or CUBIRD, then you can take advantage of their |
| 268 | AFTER and FIRST clauses to position the new column. |
| 269 | |
| 270 | Examples:: |
| 271 | |
| 272 | // Will place the new column after the `another_field` column: |
| 273 | $fields = array( |
| 274 | 'preferences' => array('type' => 'TEXT', 'after' => 'another_field') |
| 275 | ); |
| 276 | |
| 277 | // Will place the new column at the start of the table definition: |
| 278 | $fields = array( |
| 279 | 'preferences' => array('type' => 'TEXT', 'first' => TRUE) |
| 280 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 281 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 282 | |
| 283 | Dropping a Column From a Table |
| 284 | ============================== |
| 285 | |
| 286 | **$this->dbforge->drop_column()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 287 | |
| 288 | Used to remove a column from a table. |
| 289 | |
| 290 | :: |
| 291 | |
| 292 | $this->dbforge->drop_column('table_name', 'column_to_drop'); |
| 293 | |
| 294 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 295 | |
| 296 | Modifying a Column in a Table |
| 297 | ============================= |
| 298 | |
| 299 | **$this->dbforge->modify_column()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 300 | |
Andrey Andreev | eaa60c7 | 2012-11-06 01:11:22 +0200 | [diff] [blame] | 301 | The usage of this method is identical to ``add_column()``, except it |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 302 | alters an existing column rather than adding a new one. In order to |
| 303 | change the name you can add a "name" key into the field defining array. |
| 304 | |
| 305 | :: |
| 306 | |
Joseph Wensley | f24f404 | 2011-10-06 22:53:29 -0400 | [diff] [blame] | 307 | $fields = array( |
| 308 | 'old_name' => array( |
| 309 | 'name' => 'new_name', |
| 310 | 'type' => 'TEXT', |
| 311 | ), |
| 312 | ); |
| 313 | $this->dbforge->modify_column('table_name', $fields); |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 314 | // gives ALTER TABLE table_name CHANGE old_name new_name TEXT |
| 315 | |
James L Parry | d96616c | 2014-12-03 01:17:04 -0800 | [diff] [blame^] | 316 | |
| 317 | *************** |
| 318 | Class Reference |
| 319 | *************** |
| 320 | |
| 321 | .. class:: DB_forge |
| 322 | |
| 323 | .. method:: __construct(&$db) |
| 324 | |
| 325 | :param object $db: Database object |
| 326 | :returns: DB_forge object for the specified database |
| 327 | :rtype: DB_forge |
| 328 | |
| 329 | Initializes a database forge. |
| 330 | |
| 331 | .. method:: add_column($table = '', $field = array(), $_after = NULL) |
| 332 | |
| 333 | :param string $table: Table name |
| 334 | :param array $field: Column definitions |
| 335 | :param string $_after: Column for AFTER clause (deprecated) |
| 336 | :returns: TRUE on success, FALSE on failure |
| 337 | :rtype: boolean |
| 338 | |
| 339 | Add a column to a table. Usage: See `Adding a Column to a Table`_. |
| 340 | |
| 341 | .. method:: add_field($field = '') |
| 342 | |
| 343 | :param array $field: Field to add |
| 344 | :returns: DB_forge instance |
| 345 | :rtype: object |
| 346 | |
| 347 | Add a field to the set that will be used to create a table. Usage: See `Adding fields`_. |
| 348 | |
| 349 | .. method:: add_key($key = '', $primary = FALSE) |
| 350 | |
| 351 | :param array $key: Name of a key field |
| 352 | :param boolean $primary: TRUE if this key is to be a primary key |
| 353 | :returns: DB_forge instance |
| 354 | :rtype: object |
| 355 | |
| 356 | Specify a key field to be used to create a table. Usage: See `Adding Keys`_. |
| 357 | |
| 358 | .. method:: create_database($db_name) |
| 359 | |
| 360 | :param string $db_name: Name of the database to create |
| 361 | :returns: TRUE on success, FALSE on failure |
| 362 | :rtype: boolean |
| 363 | |
| 364 | Create a new database. Usage: See `Creating and Dropping Databases`_. |
| 365 | |
| 366 | .. method:: create_table($table = '', $if_not_exists = FALSE, array $attributes = array()) |
| 367 | |
| 368 | :param string $table: Name of the table to create |
| 369 | :param string $if_not_exists: TRUE to add an 'IF NOT EXISTS' clause |
| 370 | :param string $attributes: Associative array of table attributes |
| 371 | :returns: DB_driver on success, FALSE on failure |
| 372 | :rtype: mixed |
| 373 | |
| 374 | Create a new table. Usage: See `Creating a table`_. |
| 375 | |
| 376 | .. method:: drop_column($table = '', $column_name = '') |
| 377 | |
| 378 | :param string $table: Table name |
| 379 | :param array $column_name: Column to drop |
| 380 | :returns: DB_driver on success, FALSE on failure |
| 381 | :rtype: mixed |
| 382 | |
| 383 | Drop a column from a table. Usage: See `Dropping a Column From a Table`_. |
| 384 | |
| 385 | .. method:: drop_database($db_name) |
| 386 | |
| 387 | :param string $db_name: Name of the database to drop |
| 388 | :returns: TRUE on success, FALSE on failure |
| 389 | :rtype: boolean |
| 390 | |
| 391 | Drop a database. Usage: See `Creating and Dropping Databases`_. |
| 392 | |
| 393 | .. method:: drop_table($table_name, $if_exists = FALSE) |
| 394 | |
| 395 | :param string $table: Name of the table to create |
| 396 | :param string $if_exists: TRUE to add an 'IF EXISTS' clause |
| 397 | :returns: DB_driver on success, FALSE on failure |
| 398 | :rtype: mixed |
| 399 | |
| 400 | Drop a table. Usage: See `Dropping a table`_. |
| 401 | |
| 402 | .. method:: modify_column($table = '', $field = array()) |
| 403 | |
| 404 | :param string $table: Table name |
| 405 | :param array $field: Column definitions |
| 406 | :returns: TRUE on success, FALSE on failure |
| 407 | :rtype: boolean |
| 408 | |
| 409 | Modify a column in a table. Usage: See `Modifying a Column in a Table`_. |
| 410 | |
| 411 | .. method:: rename_table($table_name, $new_table_name) |
| 412 | |
| 413 | :param string $table: Name of the table |
| 414 | :param string $new_table_name: New name of the table |
| 415 | :returns: DB_driver on success, FALSE on failure |
| 416 | :rtype: mixed |
| 417 | |
| 418 | Rename a table. Usage: See `Renaming a table`_. |
| 419 | |
| 420 | |
James L Parry | 4828f89 | 2014-11-25 12:53:53 -0800 | [diff] [blame] | 421 | |