From 928c55cbc5f3f162c10077f46d75d0bc0b1cbe53 Mon Sep 17 00:00:00 2001
From: Derek Allard
Fields are created via an associative array. Within the array you must include a 'type' key that relates to the datatype of the field. For example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR) also require a 'constraint' key.
$fields = array(
'users' => array(
- 'type' => 'VARCHAR',
- 'constraint' => '100',
- ),
- );
-
+ 'type' => 'VARCHAR',
+ 'constraint' => '100',
+ ),
+ );
+
// will translate to "users VARCHAR(100)" when the field is added.
Additionally, the following key/values can be used:
$fields = array(
- 'blog_id' => array(
- 'type' => 'INT',
- 'constraint' => 5,
- 'unsigned' => TRUE,
- 'auto_increment' => TRUE
- ),
- 'blog_title' => array(
+ 'blog_id' => array(
+ 'type' => 'INT',
+ 'constraint' => 5,
+ 'unsigned' => TRUE,
+ 'auto_increment' => TRUE
+ ),
+ 'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
@@ -161,22 +161,22 @@ already be running, since the forge class relies on it.
There is a special exception for creating id fields. A field with type id will automatically be assinged as an INT(9) auto_incrementing Primary Key.
$this->dbforge->add_field('id');
- // gives id INT(9) NOT NULL AUTO_INCREMENT
Generally speaking, you'll want your table to have Keys. This is accomplished with $this->dbforge->add_key('field'). An optional second parameter set to TRUE will make it a primary key. Note that add_key() must be followed by a call to create_table().
Multiple column non-primary keys must be sent as an array. Sample output below is for MySQL.
$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`)
+ // 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`)
After fields and keys have been declared, you can create a new table with
$this->dbforge->create_table('table_name');
@@ -196,7 +196,7 @@ already be running, since the forge class relies on it.
The add_column() function is used to modify an existing table. It accepts the same field array as above, and can be used for an unlimited number of additional fields.
$fields = array(
- 'preferences' => array('type' => 'TEXT')
+ 'preferences' => array('type' => 'TEXT')
);
$this->dbforge->add_column('table_name', $fields);
@@ -213,8 +213,8 @@ $this->dbforge->add_column('table_name', $fields);
),
);
$this->dbforge->modify_column('table_name', $fields);
-
- // gives ALTER TABLE table_name CHANGE old_name new_name TEXT
-- cgit v1.2.3-24-g4f1b