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 | |
| 32 | if ($this->dbforge->create_database('my_db')) { echo 'Database created!'; } |
| 33 | |
| 34 | $this->dbforge->drop_database('db_name') |
| 35 | ========================================== |
| 36 | |
| 37 | Permits you to drop the database specified in the first parameter. |
| 38 | Returns TRUE/FALSE based on success or failure:: |
| 39 | |
| 40 | if ($this->dbforge->drop_database('my_db')) { echo 'Database deleted!'; } |
| 41 | |
| 42 | **************************** |
| 43 | Creating and Dropping Tables |
| 44 | **************************** |
| 45 | |
| 46 | There are several things you may wish to do when creating tables. Add |
| 47 | fields, add keys to the table, alter columns. CodeIgniter provides a |
| 48 | mechanism for this. |
| 49 | |
| 50 | Adding fields |
| 51 | ============= |
| 52 | |
| 53 | Fields are created via an associative array. Within the array you must |
| 54 | include a 'type' key that relates to the datatype of the field. For |
| 55 | example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR) |
| 56 | also require a 'constraint' key. |
| 57 | |
| 58 | :: |
| 59 | |
| 60 | $fields = array( 'users' => array( 'type' => 'VARCHAR', 'constraint' => '100', ), ); // will translate to "users VARCHAR(100)" when the field is added. |
| 61 | |
| 62 | |
| 63 | Additionally, the following key/values can be used: |
| 64 | |
| 65 | - unsigned/true : to generate "UNSIGNED" in the field definition. |
| 66 | - default/value : to generate a default value in the field definition. |
| 67 | - null/true : to generate "NULL" in the field definition. Without this, |
| 68 | the field will default to "NOT NULL". |
| 69 | - auto_increment/true : generates an auto_increment flag on the |
| 70 | field. Note that the field type must be a type that supports this, |
| 71 | such as integer. |
| 72 | |
| 73 | :: |
| 74 | |
| 75 | $fields = array( 'blog_id' => array( 'type' => 'INT', 'constraint' => 5, 'unsigned' => TRUE, 'auto_increment' => TRUE ), 'blog_title' => array( 'type' => 'VARCHAR', 'constraint' => '100', ), 'blog_author' => array( 'type' =>'VARCHAR', 'constraint' => '100', 'default' => 'King of Town', ), 'blog_description' => array( 'type' => 'TEXT', 'null' => TRUE, ), ); |
| 76 | |
| 77 | |
| 78 | After the fields have been defined, they can be added using |
| 79 | $this->dbforge->add_field($fields); followed by a call to the |
| 80 | create_table() function. |
| 81 | |
| 82 | $this->dbforge->add_field() |
| 83 | ---------------------------- |
| 84 | |
| 85 | The add fields function will accept the above array. |
| 86 | |
| 87 | Passing strings as fields |
| 88 | ------------------------- |
| 89 | |
| 90 | If you know exactly how you want a field to be created, you can pass the |
| 91 | string into the field definitions with add_field() |
| 92 | |
| 93 | :: |
| 94 | |
| 95 | $this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'"); |
| 96 | |
| 97 | |
| 98 | Note: Multiple calls to add_field() are cumulative. |
| 99 | |
| 100 | Creating an id field |
| 101 | -------------------- |
| 102 | |
| 103 | There is a special exception for creating id fields. A field with type |
| 104 | id will automatically be assinged as an INT(9) auto_incrementing |
| 105 | Primary Key. |
| 106 | |
| 107 | :: |
| 108 | |
| 109 | $this->dbforge->add_field('id'); // gives id INT(9) NOT NULL AUTO_INCREMENT |
| 110 | |
| 111 | |
| 112 | Adding Keys |
| 113 | =========== |
| 114 | |
| 115 | Generally speaking, you'll want your table to have Keys. This is |
| 116 | accomplished with $this->dbforge->add_key('field'). An optional second |
| 117 | parameter set to TRUE will make it a primary key. Note that add_key() |
| 118 | must be followed by a call to create_table(). |
| 119 | |
| 120 | Multiple column non-primary keys must be sent as an array. Sample output |
| 121 | below is for MySQL. |
| 122 | |
| 123 | :: |
| 124 | |
| 125 | $this->dbforge->add_key('blog_id', TRUE); // gives PRIMARY KEY `blog_id` (`blog_id`) $this->dbforge->add_key('blog_id', TRUE); $this->dbforge->add_key('site_id', TRUE); // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`) $this->dbforge->add_key('blog_name'); // gives KEY `blog_name` (`blog_name`) $this->dbforge->add_key(array('blog_name', 'blog_label')); // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`) |
| 126 | |
| 127 | |
| 128 | Creating a table |
| 129 | ================ |
| 130 | |
| 131 | After fields and keys have been declared, you can create a new table |
| 132 | with |
| 133 | |
| 134 | :: |
| 135 | |
| 136 | $this->dbforge->create_table('table_name'); // gives CREATE TABLE table_name |
| 137 | |
| 138 | |
| 139 | An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause |
| 140 | into the definition |
| 141 | |
| 142 | :: |
| 143 | |
| 144 | $this->dbforge->create_table('table_name', TRUE); // gives CREATE TABLE IF NOT EXISTS table_name |
| 145 | |
| 146 | |
| 147 | Dropping a table |
| 148 | ================ |
| 149 | |
| 150 | Executes a DROP TABLE sql |
| 151 | |
| 152 | :: |
| 153 | |
| 154 | $this->dbforge->drop_table('table_name'); // gives DROP TABLE IF EXISTS table_name |
| 155 | |
| 156 | |
| 157 | Renaming a table |
| 158 | ================ |
| 159 | |
| 160 | Executes a TABLE rename |
| 161 | |
| 162 | :: |
| 163 | |
| 164 | $this->dbforge->rename_table('old_table_name', 'new_table_name'); // gives ALTER TABLE old_table_name RENAME TO new_table_name |
| 165 | |
| 166 | |
| 167 | **************** |
| 168 | Modifying Tables |
| 169 | **************** |
| 170 | |
| 171 | $this->dbforge->add_column() |
| 172 | ============================= |
| 173 | |
| 174 | The add_column() function is used to modify an existing table. It |
| 175 | accepts the same field array as above, and can be used for an unlimited |
| 176 | number of additional fields. |
| 177 | |
| 178 | :: |
| 179 | |
| 180 | $fields = array( 'preferences' => array('type' => 'TEXT') ); $this->dbforge->add_column('table_name', $fields); // gives ALTER TABLE table_name ADD preferences TEXT |
| 181 | |
| 182 | An optional third parameter can be used to specify which existing column |
| 183 | to add the new column after. |
| 184 | |
| 185 | :: |
| 186 | |
| 187 | $this->dbforge->add_column('table_name', $fields, 'after_field'); |
| 188 | |
| 189 | |
| 190 | $this->dbforge->drop_column() |
| 191 | ============================== |
| 192 | |
| 193 | Used to remove a column from a table. |
| 194 | |
| 195 | :: |
| 196 | |
| 197 | $this->dbforge->drop_column('table_name', 'column_to_drop'); |
| 198 | |
| 199 | |
| 200 | $this->dbforge->modify_column() |
| 201 | ================================ |
| 202 | |
| 203 | The usage of this function is identical to add_column(), except it |
| 204 | alters an existing column rather than adding a new one. In order to |
| 205 | change the name you can add a "name" key into the field defining array. |
| 206 | |
| 207 | :: |
| 208 | |
| 209 | $fields = array( 'old_name' => array( 'name' => 'new_name', 'type' => 'TEXT', ), ); $this->dbforge->modify_column('table_name', $fields); // gives ALTER TABLE table_name CHANGE old_name new_name TEXT |
| 210 | |
| 211 | |
| 212 | |