add unsigned property in Model::describe

This commit is contained in:
imsamurai 2013-11-17 22:37:01 +02:00
parent 00fb663f90
commit 2f6122cb01
3 changed files with 92 additions and 2 deletions

View file

@ -348,6 +348,7 @@ class Mysql extends DboSource {
'null' => ($column->Null === 'YES' ? true : false),
'default' => $column->Default,
'length' => $this->length($column->Type),
'unsigned' => $this->unsigned($column->Type)
);
if (!empty($column->Key) && isset($this->index[$column->Key])) {
$fields[$column->Field]['key'] = $this->index[$column->Key];
@ -782,6 +783,16 @@ class Mysql extends DboSource {
return 'text';
}
/**
* Check if column type is unsigned
*
* @param string $real Real database-layer column type (i.e. "varchar(255)")
* @return bool True if column is unsigned, false otherwise
*/
public function unsigned($real) {
return strpos(strtolower($real), 'unsigned') !== false;
}
/**
* Gets the schema name
*

View file

@ -47,7 +47,7 @@ class MysqlTest extends CakeTestCase {
public $fixtures = array(
'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test',
'core.binary_test', 'core.inno'
'core.binary_test', 'core.inno', 'core.unsigned'
);
/**
@ -3165,7 +3165,7 @@ class MysqlTest extends CakeTestCase {
*
* @dataProvider buildColumnUnsignedProvider
*/
public function testBuildColumnUnsigned($data, $expected) {//debug($this->Dbo->columns);
public function testBuildColumnUnsigned($data, $expected) {
$result = $this->Dbo->buildColumn($data);
$this->assertEquals($expected, $result);
}
@ -3266,6 +3266,25 @@ class MysqlTest extends CakeTestCase {
);
}
/**
* Test getting `unsigned` field parameter from DB
*
* @return void
*/
public function testSchemaUnsigned() {
$this->loadFixtures('Unsigned');
$Model = ClassRegistry::init('Model');
$Model->setSource('unsigned');
$types = $this->Dbo->fieldParameters['unsigned']['types'];
$schema = $Model->schema();
foreach ($types as $type) {
$this->assertArrayHasKey('unsigned', $schema['u'.$type]);
$this->assertTrue($schema['u'.$type]['unsigned']);
$this->assertArrayHasKey('unsigned', $schema[$type]);
$this->assertFalse($schema[$type]['unsigned']);
}
}
/**
* test hasAny()
*

View file

@ -0,0 +1,60 @@
<?php
/**
* Short description for file.
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Fixture
* @since CakePHP(tm) v 1.2.0.4667
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Short description for class.
*
* @package Cake.Test.Fixture
*/
class UnsignedFixture extends CakeTestFixture {
/**
* table property
*
* @var array
*/
public $table = 'unsigned';
/**
* fields property
*
* @var array
*/
public $fields = array(
'uinteger' => array('type' => 'integer', 'null' => '', 'default' => '1', 'length' => '8', 'key' => 'primary', 'unsigned' => true),
'integer' => array('type' => 'integer', 'length' => '8', 'unsigned' => false),
'udecimal' => array('type' => 'decimal', 'length' => '4', 'unsigned' => true),
'decimal' => array('type' => 'decimal', 'length' => '4'),
'biginteger' => array('type' => 'biginteger', 'length' => '20', 'default' => 3),
'ubiginteger' => array('type' => 'biginteger', 'length' => '20', 'default' => 3, 'unsigned' => true),
'float' => array('type' => 'float', 'length' => '4'),
'ufloat' => array('type' => 'float', 'length' => '4', 'unsigned' => true),
'tableParameters' => array(
'engine' => 'MyISAM'
)
);
/**
* records property
*
* @var array
*/
public $records = array();
}