Skip to content

Commit

Permalink
Merge 807fa2e into b92bdc7
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Nov 12, 2018
2 parents b92bdc7 + 807fa2e commit ee2c811
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
20 changes: 7 additions & 13 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Forge
/**
* List of foreign keys.
*
* @var type
* @var array
*/
protected $foreignKeys = [];

Expand Down Expand Up @@ -340,7 +340,7 @@ public function addUniqueKey($key)
/**
* Add Field
*
* @param array $field
* @param array|string $field
*
* @return Forge
*/
Expand Down Expand Up @@ -946,19 +946,13 @@ protected function _processFields($create_table = false)

if (isset($attributes['TYPE']) && ! empty($attributes['CONSTRAINT']))
{
switch (strtoupper($attributes['TYPE']))
if (is_array($attributes['CONSTRAINT']))
{
case 'ENUM':
case 'SET':
$attributes['CONSTRAINT'] = $this->db->escapeString($attributes['CONSTRAINT']);
$field['length'] = is_array($attributes['CONSTRAINT']) ? "('" . implode("','",
$attributes['CONSTRAINT']) . "')" : '(' . $attributes['CONSTRAINT'] . ')';
break;
default:
$field['length'] = is_array($attributes['CONSTRAINT']) ? '(' . implode(',',
$attributes['CONSTRAINT']) . ')' : '(' . $attributes['CONSTRAINT'] . ')';
break;
$attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']);
$attributes['CONSTRAINT'] = implode(',', $attributes['CONSTRAINT']);
}

$field['length'] = '(' . $attributes['CONSTRAINT'] . ')';
}

$fields[] = $field;
Expand Down
37 changes: 37 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class ForgeTest extends CIDatabaseTestCase
protected $refresh = true;

protected $seed = 'Tests\Support\Database\Seeds\CITestSeeder';
/**
* @var \CodeIgniter\Database\Forge
*/
protected $forge;

public function setUp()
{
Expand Down Expand Up @@ -318,4 +322,37 @@ public function testDropForeignKey()
$this->forge->dropTable('forge_test_invoices', true);
$this->forge->dropTable('forge_test_users', true);
}

public function testCreateTableWithArrayFieldConstraints()
{
if ($this->db->DBDriver === 'MySQLi')
{
$this->forge->addField([
'enum_string' => [
'type' => 'ENUM("a","b")',
],
'enum_array' => [
'type' => 'ENUM',
'constraint' => [
'a',
'b',
],
],
]);
$this->forge->createTable('forge_test_enum');

$fields = $this->db->getFieldData('forge_test_enum');

$this->forge->dropTable('forge_test_enum');

$this->assertEquals('enum_string', $fields[0]->name);
$this->assertEquals('enum', $fields[0]->type);
$this->assertEquals('enum_array', $fields[1]->name);
$this->assertEquals('enum', $fields[1]->type);
}
else
{
$this->expectNotToPerformAssertions();
}
}
}
31 changes: 18 additions & 13 deletions user_guide_src/source/dbmgmt/forge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ also require a 'constraint' key.
$fields = [
'users' => [
'type' => 'VARCHAR',
'constraint' => '100',
'constraint' => 100,
],
];
// will translate to "users VARCHAR(100)" when the field is added.
Expand All @@ -92,25 +92,30 @@ Additionally, the following key/values can be used:
::

$fields = [
'blog_id' => [
'id' => [
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
'unsigned' => true,
'auto_increment' => true
],
'blog_title' => [
'title' => [
'type' => 'VARCHAR',
'constraint' => '100',
'unique' => TRUE,
'unique' => true,
],
'blog_author' => [
'author' => [
'type' =>'VARCHAR',
'constraint' => '100',
'constraint' => 100,
'default' => 'King of Town',
],
'blog_description' => [
'description' => [
'type' => 'TEXT',
'null' => TRUE,
'null' => true,
],
'status' => [
'type' => 'ENUM',
'constraint' => ['publish', 'pending', 'draft'],
'default' => 'pending',
],
];

Expand All @@ -132,9 +137,9 @@ string into the field definitions with addField()

$forge->addField("label varchar(100) NOT NULL DEFAULT 'default label'");

.. note:: Passing raw strings as fields cannot be followed by ``add_key()`` calls on those fields.
.. note:: Passing raw strings as fields cannot be followed by ``addKey()`` calls on those fields.

.. note:: Multiple calls to add_field() are cumulative.
.. note:: Multiple calls to addField() are cumulative.

Creating an id field
--------------------
Expand Down Expand Up @@ -320,7 +325,7 @@ Modifying a Column in a Table

**$forge->modifyColumn()**

The usage of this method is identical to ``add_column()``, except it
The usage of this method is identical to ``addColumn()``, except it
alters an existing column rather than adding a new one. In order to
change the name you can add a "name" key into the field defining array.

Expand Down

0 comments on commit ee2c811

Please sign in to comment.