Skip to content

Commit 031f502

Browse files
committed
Adding a boolean() method
1 parent 56b16e6 commit 031f502

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Schema::create_table('users', function($table){
1717

1818
$table->text('biography');
1919

20+
$table->boolean('enabled');
21+
2022
$table->timestamps();
2123
});
2224
```
@@ -71,6 +73,7 @@ $table->create_table();
7173
* `text($name)` - create a TEXT column
7274
* `date($name)` - create a DATE column
7375
* `datetime($text)` - create a DATETIME column
76+
* `boolean($name)` - MySQL doesn't have a native boolean type, so we create a TINYINT
7477

7578
There are also two special methods to speed up your development.
7679

libraries/Schema.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Schema {
1919
* VARIABLES
2020
* ------------------------------------------------------------ */
2121

22-
static public $types = array( 'integer' => 'INT', 'string' => 'VARCHAR', 'text' => 'TEXT', 'date' => 'DATE', 'datetime' => 'DATETIME' );
22+
static public $types = array( 'integer' => 'INT', 'string' => 'VARCHAR', 'text' => 'TEXT', 'date' => 'DATE', 'datetime' => 'DATETIME', 'boolean' => 'TINYINT' );
2323

2424
/* --------------------------------------------------------------
2525
* GENERIC METHODS
@@ -176,6 +176,12 @@ public function text($column_name, $options = array()) {
176176
), $options);
177177
}
178178

179+
public function boolean($column_name, $options = array()) {
180+
$this->add_definition_rule($column_name, array(
181+
'type' => 'TINYINT'
182+
), $options);
183+
}
184+
179185
public function date($column_name, $options = array()) {
180186
$this->add_definition_rule($column_name, array(
181187
'type' => 'DATE'

tests/Schema_Table_Definition_test.php

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public function test_datetime() {
8888
);
8989
}
9090

91+
public function test_boolean() {
92+
$schema_table_definition = new Schema_Table_Definition('table_name');
93+
$schema_table_definition->boolean('column_name', array( 'option' => 'here' ));
94+
95+
$this->assert_equal($schema_table_definition->columns(), array(
96+
'column_name' => array('type' => 'TINYINT', 'option' => 'here')
97+
));
98+
}
99+
91100
public function test_timestamps() {
92101
$schema_table_definition = new Schema_Table_Definition('table_name');
93102
$schema_table_definition->timestamps();

0 commit comments

Comments
 (0)