diff --git a/src/Database/Type.php b/src/Database/Type.php index f4393b8fe72..7240f5ba204 100644 --- a/src/Database/Type.php +++ b/src/Database/Type.php @@ -51,8 +51,8 @@ class Type * @var array */ protected static $_basicTypes = [ - 'string' => ['callback' => 'strval'], - 'text' => ['callback' => 'strval'], + 'string' => ['callback' => ['\Cake\Database\Type', 'strval']], + 'text' => ['callback' => ['\Cake\Database\Type', 'strval']], 'boolean' => [ 'callback' => ['\Cake\Database\Type', 'boolval'], 'pdo' => PDO::PARAM_BOOL @@ -186,10 +186,6 @@ protected function _basicTypeCast($value) if ($value === null) { return null; } - if (is_array($value)) { - $value = ''; - } - if (!empty(self::$_basicTypes[$this->_name])) { $typeInfo = self::$_basicTypes[$this->_name]; if (isset($typeInfo['callback'])) { @@ -236,6 +232,22 @@ public static function boolval($value) return !empty($value); } + /** + * Type converter for string values. + * + * Will convert values into strings + * + * @param mixed $value The value to convert to a string. + * @return bool + */ + public static function strval($value) + { + if (is_array($value)) { + $value = ''; + } + return strval($value); + } + /** * Generate a new primary key value for a given type. * diff --git a/tests/TestCase/Database/TypeTest.php b/tests/TestCase/Database/TypeTest.php index 786a5249a11..44f5d1692c1 100644 --- a/tests/TestCase/Database/TypeTest.php +++ b/tests/TestCase/Database/TypeTest.php @@ -261,7 +261,7 @@ public function testBooleanToDatabase() $this->assertFalse($type->toDatabase(0, $driver)); $this->assertTrue($type->toDatabase('1', $driver)); $this->assertFalse($type->toDatabase('0', $driver)); - $this->assertFalse($type->toDatabase([1, 2], $driver)); + $this->assertTrue($type->toDatabase([1, 2], $driver)); } /** @@ -299,7 +299,7 @@ public function testBooleanToPHP() $this->assertFalse($type->toPHP('0', $driver)); $this->assertFalse($type->toPHP('FALSE', $driver)); $this->assertFalse($type->toPHP('false', $driver)); - $this->assertFalse($type->toPHP(['2', '3'], $driver)); + $this->assertTrue($type->toPHP(['2', '3'], $driver)); } /** @@ -320,7 +320,7 @@ public function testBooleanMarshal() $this->assertFalse($type->marshal(0)); $this->assertFalse($type->marshal('')); $this->assertFalse($type->marshal('invalid')); - $this->assertFalse($type->marshal(['2', '3'])); + $this->assertTrue($type->marshal(['2', '3'])); }