|
| 1 | +--- |
| 2 | +lang: php |
| 3 | +--- |
| 4 | + |
| 5 | +<!-- |
| 6 | + This source file is part of the open source project |
| 7 | + ExpressionEngine User Guide (https://github.com/ExpressionEngine/ExpressionEngine-User-Guide) |
| 8 | +
|
| 9 | + @link https://expressionengine.com/ |
| 10 | + @copyright Copyright (c) 2003-2025, Packet Tide, LLC (https://packettide.com) |
| 11 | + @license https://expressionengine.com/license Licensed under Apache License, Version 2.0 |
| 12 | +--> |
| 13 | + |
| 14 | +# Database SmartForge Class |
| 15 | + |
| 16 | +**class `Smartforge`** |
| 17 | + |
| 18 | +[TOC] |
| 19 | + |
| 20 | +The Smartforge Class contains utility methods to help you manage your database. Load the Forge Class as follows: |
| 21 | + |
| 22 | + ee()->load->library('smartforge') |
| 23 | + |
| 24 | +Once initialized you will access the methods using the `ee()->smartforge` object: |
| 25 | + |
| 26 | + ee()->smartforge->some_method(); |
| 27 | + |
| 28 | +Some Smartforge class functions duplicate similar dbforge class functions but adds more intelligence to these transaction - for example by checking that the |
| 29 | +table / column / index actually exist before executing the function. The other Smartforge functions are novel to the class. |
| 30 | + |
| 31 | +## Utility functions |
| 32 | + |
| 33 | +[TOC=3] |
| 34 | + |
| 35 | +### `create_table($table)` |
| 36 | + |
| 37 | +| Parameter | Type | Description | |
| 38 | +| --------- | -------------- | ----------------------------------------- | |
| 39 | +| \$table | `String` | The name of the table to create | |
| 40 | +| Returns | `CI_DB_result` | The result of the `CREATE TABLE` query | |
| 41 | + |
| 42 | +Creates a table within the current EE database. Function will check to make sure the existing table doesn't exist before creating it. |
| 43 | + |
| 44 | + ee()->smartforge->create_table('cat_watch_list'); |
| 45 | + |
| 46 | +### `rename_table($table, $new_table)` |
| 47 | + |
| 48 | +| Parameter | Type | Description | |
| 49 | +| ----------- | ---------------------- | -------------------------------------------------------------- | |
| 50 | +| \$table | `String` | The name of the table to rename | |
| 51 | +| \$new_table | `String` | The new name for the table. | |
| 52 | +| Returns | `CI_DB_result| bool` | The result of the `ALTER TABLE .. RENAME TO ..` query or false | |
| 53 | + |
| 54 | +Renames a table. Function will check to make sure the existing table name actually exists and the new table name doesn't. |
| 55 | + |
| 56 | + ee()->smartforge->rename_table('cat_watch_list','malicious_feline_ids'); |
| 57 | + |
| 58 | +### `add_column($table = '', $field = array(), $after_field = '')` |
| 59 | + |
| 60 | +| Parameter | Type | Description | |
| 61 | +| -------------- | ---------------------- | -------------------------------------------------------------- | |
| 62 | +| \$table | `String` | The name of the table to add a column to | |
| 63 | +| \$field | `Array` | A multiddimensional associative array containing field names as the keys and an associative array of parameters for creating database fields: <br> `type`: The type of field to create (e.g. `INT`, `VARCHAR`, `TEXT`) <br> `constraint`: The length of the field <br> `unsigned`: Set to `TRUE` to generate `UNSIGNED` in the field definition. <br> `default`: Set to a value to generate a default value in the field definition. <br> `null`: Set to `TRUE` to generate `NULL` in the field definition. Without this, the field will default to `NOT NULL`. <br> `auto_increment`: Set to `TRUE` to generate an `auto_increment` flag on the field. Note that the field type must be a type that supports this, such as integer. | |
| 64 | +| \$after_field | `String` | The field that should come before this new field, leave empty to be the last field | |
| 65 | +| Returns | `bool` | True if the column add succeeds, otherwise false | |
| 66 | + |
| 67 | +Adds one or more columns to a table, optionally starting the insertion after a nominated column. For each column to add it checks to see if column already exists in the DB; if it does it skips adding that column. |
| 68 | + |
| 69 | + ee()->smartforge->add_column('cat_watch_list','malicious_feline_ids'); |
| 70 | + $fields = array( |
| 71 | + 'preferences' => array('type' => 'TEXT') |
| 72 | + ); |
| 73 | + ee()->smartforge->add_column('table_name', $fields); |
| 74 | + |
| 75 | +You can also take advantage of MySQL's `AFTER` and `FIRST` clauses to position the new column: |
| 76 | + |
| 77 | + // Will place the new column after the `another_field` column: |
| 78 | + $fields = array( |
| 79 | + 'preferences' => array('type' => 'TEXT', 'after' => 'another_field') |
| 80 | + ); |
| 81 | + |
| 82 | + // Will place the new column at the start of the table definition: |
| 83 | + $fields = array( |
| 84 | + 'preferences' => array('type' => 'TEXT', 'first' => TRUE) |
| 85 | + ); |
| 86 | + |
| 87 | +### `drop_column($table, $column_name)` |
| 88 | + |
| 89 | +| Parameter | Type | Description | |
| 90 | +| ------------- | -------------- | --------------------------------- | |
| 91 | +| \$table | `String` | The table to drop the column from | |
| 92 | +| \$column_name | `String` | The name of column to drop | |
| 93 | +| Returns | `CI_DB_result|bool` | The result of the `DROP` query or false if it fails | |
| 94 | + |
| 95 | +Drop a column in the given database table if it already exists. |
| 96 | + |
| 97 | + ee()->smartforge->drop_column('table_name', 'name_of_col_7'); |
| 98 | + |
| 99 | +### `drop_column_batch($table, $column_names)` |
| 100 | + |
| 101 | +| Parameter | Type | Description | |
| 102 | +| ------------- | -------------- | --------------------------------- | |
| 103 | +| \$table | `String` | The table to drop the column from | |
| 104 | +| \$column_names | `Array` | An array of column names to drop | |
| 105 | +| Returns | `CI_DB_result|bool` | The result of the `DROP` query or false if it fails | |
| 106 | + |
| 107 | +Drop multiple columns in the given database table if they already exists. |
| 108 | + |
| 109 | + ee()->smartforge->drop_column('table_name', ['name_of_col_11','name_of_col_33']); |
| 110 | + |
| 111 | +### `drop_table($table)` |
| 112 | + |
| 113 | +| Parameter | Type | Description | |
| 114 | +| --------- | -------------- | ---------------------------------------------- | |
| 115 | +| \$table | `String` | The name of the table to drop | |
| 116 | +| Returns | `CI_DB_result|bool` | The result of the `DROP TABLE IF EXISTS` query or false if it fails| |
| 117 | + |
| 118 | +Drops a table from the database if it exists. |
| 119 | + |
| 120 | + ee()->smartforge->drop_table('table_name'); |
| 121 | + |
| 122 | +### `modify_column($table = '', $field = array())` |
| 123 | + |
| 124 | +| Parameter | Type | Description | |
| 125 | +| --------- | -------------- | ------------------------------------------------------ | |
| 126 | +| \$table | `String` | The table to add the column to | |
| 127 | +| \$field | `Array` | The column definition (see `add_column()` for details) | |
| 128 | +| Returns | `bool` | True if it succeeds, false otherwise | |
| 129 | + |
| 130 | + Modify a database column (if it exists) with the added check that if the column is being renamed and both current column (A) and proposed column (B) names exist, drop column A and leave column B. |
| 131 | + |
| 132 | +If both columns exist, it's likely this update is being run again from a version further back than the point the DB is actually at (an overlay, if you will). Therefore, column B is probably the one with all the data in it, and column A has only very recently (as in, within seconds) been created. |
| 133 | + |
| 134 | + $fields = array( |
| 135 | + 'old_name' => array( |
| 136 | + 'name' => 'new_name', |
| 137 | + 'type' => 'TEXT', |
| 138 | + ), |
| 139 | + ); |
| 140 | + |
| 141 | + ee()->smartforge->modify_column('table_name', $fields); |
| 142 | + |
| 143 | +### `insert_set($table = '', $values = array(), $unique = array())` |
| 144 | + |
| 145 | +| Parameter | Type | Description | |
| 146 | +| --------- | -------------- | ------------------------------------------------------ | |
| 147 | +| \$table | `String` | The table to add the column to | |
| 148 | +| \$values | `Array` | An associative array of column names => row values | |
| 149 | +| \$unique | `Array` | An associative array of column names => row values (can only include key/value pairs from $values) | |
| 150 | +| Returns | `bool` | True if it succeeds, false otherwise | |
| 151 | + |
| 152 | +Insert values into the database, with optional unique column name/values in a given column(s). |
| 153 | + |
| 154 | +If the unique field content already exists in the column in the DB, function return FALSE since this set of values cannot be inserted into the column without breaking the uniqueness test. |
| 155 | + |
| 156 | + ee()->smartforge->insert_set('table_name', $values, $unique); |
| 157 | + |
| 158 | +### `add_key($table = '', $col_name = '', $key_name = '')` |
| 159 | + |
| 160 | +| Parameter | Type | Description | |
| 161 | +| ------------ | -------------- | ------------------------------------------------ | |
| 162 | +| \$table | `String` | The name of the table to drop | |
| 163 | +| \$col_name | `String|array` | The name(s) of the column(s) to include in index | |
| 164 | +| \$key_name | `String` | The name for the new key (optional) | |
| 165 | +| Returns | `bool` | True if it succeeds, false otherwise | |
| 166 | + |
| 167 | +Add a new key to the given database table if it doesn't already exist. Will create a composite primary key if `$col_name` is array and key name is PRIMARY. |
| 168 | + |
| 169 | + ee()->smartforge->add_key('table_name', 'name_of_column_22', 'index_for_22'); |
| 170 | + |
| 171 | +### `drop_key($table = '', $key_name = '')` |
| 172 | + |
| 173 | +| Parameter | Type | Description | |
| 174 | +| ------------ | -------------- | ------------------------------------------------ | |
| 175 | +| \$table | `String` | The name of the table to drop | |
| 176 | +| \$key_name | `String` | The name of the key to drop | |
| 177 | +| Returns | `bool` | True if it succeeds, false otherwise | |
| 178 | + |
| 179 | +Add a new key to the given database table if it doesn't already exist. Will create a composite primary key if `$col_name` is array and key name is PRIMARY. |
| 180 | + |
| 181 | + ee()->smartforge->drop_key('table_name', 'index_for_22'); |
| 182 | + |
0 commit comments