diff --git a/src/Database/Schema/Table.php b/src/Database/Schema/Table.php index 05c153ee1ba..d42af053d26 100644 --- a/src/Database/Schema/Table.php +++ b/src/Database/Schema/Table.php @@ -16,6 +16,7 @@ use Cake\Database\Connection; use Cake\Database\Exception; +use Cake\Database\Type; /** * Represents a single table in a database schema. @@ -81,6 +82,7 @@ class Table */ protected static $_columnKeys = [ 'type' => null, + 'baseType' => null, 'length' => null, 'precision' => null, 'null' => null, @@ -341,6 +343,34 @@ public function columnType($name, $type = null) return $this->_columns[$name]['type']; } + /** + * Returns the base type name for the provided column. + * This represent the database type a more complex class is + * based upon. + * + * @param string $column The column name to get the base type from + * @return string The base type name + */ + public function baseColumnType($column) + { + if (isset($this->_columns[$column]['baseType'])) { + return $this->_columns[$column]['baseType']; + } + + $type = $this->columnType($column); + + if ($type === null) { + return null; + } + + $map = Type::map($type); + + if (isset($map[$type])) { + $type = Type::build($type)->getBaseType(); + } + return $this->_columns[$column]['baseType'] = $type; + } + /** * Check whether or not a field is nullable * diff --git a/src/View/Form/EntityContext.php b/src/View/Form/EntityContext.php index 69a18e29662..84e2ed82288 100644 --- a/src/View/Form/EntityContext.php +++ b/src/View/Form/EntityContext.php @@ -456,7 +456,7 @@ public function type($field) { $parts = explode('.', $field); $table = $this->_getTable($parts); - return $table->schema()->columnType(array_pop($parts)); + return $table->schema()->baseColumnType(array_pop($parts)); } /** diff --git a/tests/TestCase/Database/Schema/TableTest.php b/tests/TestCase/Database/Schema/TableTest.php index 8c24512632f..23102f42f43 100644 --- a/tests/TestCase/Database/Schema/TableTest.php +++ b/tests/TestCase/Database/Schema/TableTest.php @@ -15,10 +15,24 @@ namespace Cake\Test\TestCase\Database\Schema; use Cake\Database\Schema\Table; +use Cake\Database\Type; use Cake\Datasource\ConnectionManager; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; +/** + * Mock class for testing baseType inheritance + * + */ +class FooType extends Type +{ + + public function getBaseType() + { + return 'integer'; + } +} + /** * Test case for Table */ @@ -132,6 +146,41 @@ public function testColumnTypeSet() $this->assertEquals('json', $table->columnType('title')); } + /** + * Tests getting the baseType as configured when creating the column + * + * @return void + */ + public function testBaseColumnType() + { + $table = new Table('articles'); + $table->addColumn('title', [ + 'type' => 'json', + 'baseType' => 'text', + 'length' => 25, + 'null' => false + ]); + $this->assertEquals('json', $table->columnType('title')); + $this->assertEquals('text', $table->baseColumnType('title')); + } + + /** + * Tests getting the base type as it is retuned by the Type class + * + * @return void + */ + public function testBaseColumnTypeInherited() + { + Type::map('foo', __NAMESPACE__ . '\FooType'); + $table = new Table('articles'); + $table->addColumn('thing', [ + 'type' => 'foo', + 'null' => false + ]); + $this->assertEquals('foo', $table->columnType('thing')); + $this->assertEquals('integer', $table->baseColumnType('thing')); + } + /** * Attribute keys should be filtered and have defaults set. * @@ -146,6 +195,7 @@ public function testAddColumnFiltersAttributes() $result = $table->column('title'); $expected = [ 'type' => 'string', + 'baseType' => null, 'length' => null, 'precision' => null, 'default' => null, @@ -161,6 +211,7 @@ public function testAddColumnFiltersAttributes() $result = $table->column('author_id'); $expected = [ 'type' => 'integer', + 'baseType' => null, 'length' => null, 'precision' => null, 'default' => null, @@ -177,6 +228,7 @@ public function testAddColumnFiltersAttributes() $result = $table->column('amount'); $expected = [ 'type' => 'decimal', + 'baseType' => null, 'length' => null, 'precision' => null, 'default' => null, diff --git a/tests/TestCase/View/Form/EntityContextTest.php b/tests/TestCase/View/Form/EntityContextTest.php index 65fef2ecc57..46169960145 100644 --- a/tests/TestCase/View/Form/EntityContextTest.php +++ b/tests/TestCase/View/Form/EntityContextTest.php @@ -1076,7 +1076,7 @@ protected function _setupTables() 'id' => ['type' => 'integer', 'length' => 11, 'null' => false], 'title' => ['type' => 'string', 'length' => 255], 'user_id' => ['type' => 'integer', 'length' => 11, 'null' => false], - 'body' => ['type' => 'text'] + 'body' => ['type' => 'crazy_text', 'baseType' => 'text'] ]); $users->schema([ 'id' => ['type' => 'integer', 'length' => 11],