Skip to content

Commit 2cf9727

Browse files
committed
Don't replace array data for custom types.
In the comments attached to 7468434, a few people mentioned that those changes broke their custom types. This change moves the forced casting of array data into the string/text handling only. The original intention was not to disturb/break custom type objects. As a side effect of this change boolean handling now better reflects the internal PHP handling of values in that arrays are truthy.
1 parent e205a0a commit 2cf9727

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/Database/Type.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class Type
5151
* @var array
5252
*/
5353
protected static $_basicTypes = [
54-
'string' => ['callback' => 'strval'],
55-
'text' => ['callback' => 'strval'],
54+
'string' => ['callback' => ['\Cake\Database\Type', 'strval']],
55+
'text' => ['callback' => ['\Cake\Database\Type', 'strval']],
5656
'boolean' => [
5757
'callback' => ['\Cake\Database\Type', 'boolval'],
5858
'pdo' => PDO::PARAM_BOOL
@@ -186,10 +186,6 @@ protected function _basicTypeCast($value)
186186
if ($value === null) {
187187
return null;
188188
}
189-
if (is_array($value)) {
190-
$value = '';
191-
}
192-
193189
if (!empty(self::$_basicTypes[$this->_name])) {
194190
$typeInfo = self::$_basicTypes[$this->_name];
195191
if (isset($typeInfo['callback'])) {
@@ -236,6 +232,22 @@ public static function boolval($value)
236232
return !empty($value);
237233
}
238234

235+
/**
236+
* Type converter for string values.
237+
*
238+
* Will convert values into strings
239+
*
240+
* @param mixed $value The value to convert to a string.
241+
* @return bool
242+
*/
243+
public static function strval($value)
244+
{
245+
if (is_array($value)) {
246+
$value = '';
247+
}
248+
return strval($value);
249+
}
250+
239251
/**
240252
* Generate a new primary key value for a given type.
241253
*

tests/TestCase/Database/TypeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function testBooleanToDatabase()
261261
$this->assertFalse($type->toDatabase(0, $driver));
262262
$this->assertTrue($type->toDatabase('1', $driver));
263263
$this->assertFalse($type->toDatabase('0', $driver));
264-
$this->assertFalse($type->toDatabase([1, 2], $driver));
264+
$this->assertTrue($type->toDatabase([1, 2], $driver));
265265
}
266266

267267
/**
@@ -299,7 +299,7 @@ public function testBooleanToPHP()
299299
$this->assertFalse($type->toPHP('0', $driver));
300300
$this->assertFalse($type->toPHP('FALSE', $driver));
301301
$this->assertFalse($type->toPHP('false', $driver));
302-
$this->assertFalse($type->toPHP(['2', '3'], $driver));
302+
$this->assertTrue($type->toPHP(['2', '3'], $driver));
303303
}
304304

305305
/**
@@ -320,7 +320,7 @@ public function testBooleanMarshal()
320320
$this->assertFalse($type->marshal(0));
321321
$this->assertFalse($type->marshal(''));
322322
$this->assertFalse($type->marshal('invalid'));
323-
$this->assertFalse($type->marshal(['2', '3']));
323+
$this->assertTrue($type->marshal(['2', '3']));
324324
}
325325

326326

0 commit comments

Comments
 (0)