Skip to content
Permalink
Browse files

Reading baseColumnType() insted of columnType() in EntityContext

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 e726b0d91d7d6b186af8ac089e47259e56c13794
@@ -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
*
@@ -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));
}
/**
@@ -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,
@@ -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],

0 comments on commit e726b0d

Please sign in to comment.
You can’t perform that action at this time.