diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index ba790a945..aed75f382 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -367,6 +367,18 @@ class DboSource extends DataSource { $column = $this->introspectType($data); } + $isStringEnum = false; + if (strpos($column, "enum") === 0) { + $firstValue = null; + if (preg_match("/(enum\()(.*)(\))/i", $column, $acceptingValues)) { + $values = explode(",", $acceptingValues[2]); + $firstValue = $values[0]; + } + if (is_string($firstValue)) { + $isStringEnum = true; + } + } + switch ($column) { case 'binary': return $this->_connection->quote($data, PDO::PARAM_LOB); @@ -382,11 +394,12 @@ class DboSource extends DataSource { if (is_float($data)) { return str_replace(',', '.', strval($data)); } - if ((is_int($data) || $data === '0') || ( + if (((is_int($data) || $data === '0') || ( is_numeric($data) && strpos($data, ',') === false && $data[0] != '0' && strpos($data, 'e') === false) + ) && !$isStringEnum ) { return $data; } diff --git a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php index 9b9e9f00f..2d206d5bf 100644 --- a/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php +++ b/lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php @@ -1028,6 +1028,11 @@ class DboSourceTest extends CakeTestCase { $this->markTestSkipped('This test can only run on MySQL'); } $name = $this->db->fullTableName('enum_tests'); + + $query = "DROP TABLE IF EXISTS {$name};"; + $result = $this->db->query($query); + $this->assertTrue($result); + $query = "CREATE TABLE {$name} (mood ENUM('','happy','sad','ok') NOT NULL);"; $result = $this->db->query($query); $this->assertTrue($result); @@ -1047,6 +1052,40 @@ class DboSourceTest extends CakeTestCase { ), $enumResult); } +/** + * Test for MySQL enum datatype for a list of Integer stored as String + * + * @return void + */ + public function testIntValueAsStringOnEnum() { + if (!$this->db instanceof Mysql) { + $this->markTestSkipped('This test can only run on MySQL'); + } + $name = $this->db->fullTableName('enum_faya_tests'); + + $query = "DROP TABLE IF EXISTS {$name};"; + $result = $this->db->query($query); + $this->assertTrue($result); + + $query = "CREATE TABLE {$name} (faya enum('10','20','30','40') NOT NULL);"; + $result = $this->db->query($query); + $this->assertTrue($result); + + $EnumFayaTest = ClassRegistry::init('EnumFayaTest'); + $enumResult = $EnumFayaTest->save(array('faya' => '10')); + + $query = "DROP TABLE {$name};"; + $result = $this->db->query($query); + $this->assertTrue($result); + + $this->assertEquals(array( + 'EnumFayaTest' => array( + 'faya' => '10', + 'id' => '0' + ) + ), $enumResult); + } + /** * test order to generate query order clause for virtual fields *