Skip to content

Commit

Permalink
Making Table::columnType() a setter too
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 6, 2014
1 parent 804ad32 commit 669e5c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Database/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,11 @@ class Table {
*/
const ACTION_RESTRICT = 'restrict';

/**
* Foreign key restrict default
*
* @var string
*/
const ACTION_SET_DEFAULT = 'setDefault';

/**
Expand Down Expand Up @@ -312,15 +317,20 @@ public function column($name) {
}

/**
* Convenience method for getting the type of a given column.
* Sets the type of a column, or returns its current type
* if none is passed.
*
* @param string $name The column to get the type of.
* @param string $type The type to set the column to.
* @return string|null Either the column type or null.
*/
public function columnType($name) {
public function columnType($name, $type = null) {
if (!isset($this->_columns[$name])) {
return null;
}
if ($type !== null) {
$this->_columns[$name]['type'] = $type;
}
return $this->_columns[$name]['type'];
}

Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Database/Schema/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ public function testColumnType() {
$this->assertNull($table->columnType('not there'));
}

/**
* Test columnType setter method
*
* @return void
*/
public function testColumnTypeSet() {
$table = new Table('articles');
$table->addColumn('title', [
'type' => 'string',
'length' => 25,
'null' => false
]);
$this->assertEquals('string', $table->columnType('title'));
$table->columnType('title', 'json');
$this->assertEquals('json', $table->columnType('title'));
}

/**
* Attribute keys should be filtered and have defaults set.
*
Expand Down

0 comments on commit 669e5c9

Please sign in to comment.