From a1b3ebb96147b96b2d7c2b723e7c69dcd41130f9 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 11 Jun 2013 21:54:47 -0400 Subject: [PATCH] Add Table::columnType() accessor Since this method is used a few times in the ORM package add a simple way to read it. --- lib/Cake/Database/Schema/Table.php | 13 +++++++++++++ .../Test/TestCase/Database/Schema/TableTest.php | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Cake/Database/Schema/Table.php b/lib/Cake/Database/Schema/Table.php index 9d15153aeb9..3e154b5d5be 100644 --- a/lib/Cake/Database/Schema/Table.php +++ b/lib/Cake/Database/Schema/Table.php @@ -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. * diff --git a/lib/Cake/Test/TestCase/Database/Schema/TableTest.php b/lib/Cake/Test/TestCase/Database/Schema/TableTest.php index c97fda5ee4a..e8d86137483 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/TableTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/TableTest.php @@ -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. *