Skip to content

Commit

Permalink
Reading baseColumnType() insted of columnType() in EntityContext
Browse files Browse the repository at this point in the history
This helps users with custom databse types to still use the form helper
with ease.
  • Loading branch information
lorenzo committed Jul 9, 2015
1 parent 8488eed commit e726b0d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/Database/Schema/Table.php
Expand Up @@ -16,6 +16,7 @@

use Cake\Database\Connection;
use Cake\Database\Exception;
use Cake\Database\Type;

/**
* Represents a single table in a database schema.
Expand Down Expand Up @@ -81,6 +82,7 @@ class Table
*/
protected static $_columnKeys = [
'type' => null,
'baseType' => null,
'length' => null,
'precision' => null,
'null' => null,
Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion src/View/Form/EntityContext.php
Expand Up @@ -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));
}

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/TestCase/Database/Schema/TableTest.php
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand All @@ -146,6 +195,7 @@ public function testAddColumnFiltersAttributes()
$result = $table->column('title');
$expected = [
'type' => 'string',
'baseType' => null,
'length' => null,
'precision' => null,
'default' => null,
Expand All @@ -161,6 +211,7 @@ public function testAddColumnFiltersAttributes()
$result = $table->column('author_id');
$expected = [
'type' => 'integer',
'baseType' => null,
'length' => null,
'precision' => null,
'default' => null,
Expand All @@ -177,6 +228,7 @@ public function testAddColumnFiltersAttributes()
$result = $table->column('amount');
$expected = [
'type' => 'decimal',
'baseType' => null,
'length' => null,
'precision' => null,
'default' => null,
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/View/Form/EntityContextTest.php
Expand Up @@ -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],
Expand Down

0 comments on commit e726b0d

Please sign in to comment.