Navigation Menu

Skip to content

Commit

Permalink
Add Table::columnType() accessor
Browse files Browse the repository at this point in the history
Since this method is used a few times in the ORM package add a simple
way to read it.
  • Loading branch information
markstory committed Jun 12, 2013
1 parent 48bb198 commit a1b3ebb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Cake/Database/Schema/Table.php
Expand Up @@ -209,6 +209,19 @@ public function column($name) {
return $this->_columns[$name];
}

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

/**
* Add an index.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/Cake/Test/TestCase/Database/Schema/TableTest.php
Expand Up @@ -64,6 +64,22 @@ public function testAddColumn() {
$this->assertEquals(['title', 'body'], $table->columns());
}

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

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

0 comments on commit a1b3ebb

Please sign in to comment.