diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 4821c2c6a..e99639d09 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -915,7 +915,7 @@ class DataMapperAbstract implements DataMapperInterface } elseif ($type === 'bool') { return (bool) $value; } elseif ($type === 'DateTime') { - return $value->format('Y-m-d H:i:s'); + return $value === null ? null : $value->format('Y-m-d H:i:s'); } elseif ($type === 'Json' || $type === 'jsonSerializable') { return (string) \json_encode($value); } elseif ($type === 'Serializable') { @@ -2003,7 +2003,7 @@ class DataMapperAbstract implements DataMapperInterface $refProp->setValue($obj, $value); } elseif (static::$columns[$column]['type'] === 'DateTime') { - $value = new \DateTime($value ?? ''); + $value = $value === null ? null : new \DateTime($value); if ($hasPath) { $value = ArrayUtils::setArray($arrayPath, $aValue, $value, '/', true); } @@ -2055,7 +2055,7 @@ class DataMapperAbstract implements DataMapperInterface if (\in_array(static::$columns[$column]['type'], ['string', 'int', 'float', 'bool'])) { \settype($value, static::$columns[$column]['type']); } elseif (static::$columns[$column]['type'] === 'DateTime') { - $value = new \DateTime($value ?? ''); + $value = $value === null ? null : new \DateTime($value); } elseif (static::$columns[$column]['type'] === 'Json') { $value = \json_decode($value, true); } diff --git a/tests/DataStorage/Database/DataMapperAbstractTest.php b/tests/DataStorage/Database/DataMapperAbstractTest.php index 409ebf1c4..2d04a5d30 100644 --- a/tests/DataStorage/Database/DataMapperAbstractTest.php +++ b/tests/DataStorage/Database/DataMapperAbstractTest.php @@ -43,6 +43,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase } }, 'datetime' => new \DateTime('2005-10-11'), + 'datetime_null' => null, 'ownsOneSelf' => [ 'id' => 0, 'string' => 'OwnsOne', @@ -88,6 +89,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase `test_base_json` varchar(254) DEFAULT NULL, `test_base_json_serializable` varchar(254) DEFAULT NULL, `test_base_datetime` datetime DEFAULT NULL, + `test_base_datetime_null` datetime DEFAULT NULL, /* There was a bug where it returned the current date because new \DateTime(null) === current date which is wrong, we want null as value! */ PRIMARY KEY (`test_base_id`) )ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;' )->execute(); @@ -176,6 +178,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase self::assertEquals($this->model->float, $modelR->float); self::assertEquals($this->model->null, $modelR->null); self::assertEquals($this->model->datetime->format('Y-m-d'), $modelR->datetime->format('Y-m-d')); + self::assertEquals(null, $modelR->datetime_null); // todo implement these //self::assertEquals('123', $modelR->serializable); @@ -207,6 +210,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase self::assertEquals($this->modelArray['float'], $modelR['float']); self::assertEquals($this->modelArray['null'], $modelR['null']); self::assertEquals($this->modelArray['datetime']->format('Y-m-d'), $modelR['datetime']->format('Y-m-d')); + self::assertEquals(null, $modelR['datetime_null']); self::assertCount(2, $modelR['hasManyDirect']); self::assertCount(2, $modelR['hasManyRelations']); @@ -232,6 +236,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase $modelR->float = 3.15; $modelR->null = null; $modelR->datetime = new \DateTime('now'); + $modelR->datetime_null = null; $id2 = BaseModelMapper::update($modelR); $modelR2 = BaseModelMapper::get($id2); @@ -242,6 +247,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase self::assertEquals($modelR->float, $modelR2->float); self::assertEquals($modelR->null, $modelR2->null); self::assertEquals($modelR->datetime->format('Y-m-d'), $modelR2->datetime->format('Y-m-d')); + self::assertEquals(null, $modelR2->datetime_null); // todo test update relations } @@ -257,6 +263,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase $modelR['float'] = 3.15; $modelR['null'] = null; $modelR['datetime'] = new \DateTime('now'); + $modelR['datetime_null'] = null; $id2 = BaseModelMapper::updateArray($modelR); $modelR2 = BaseModelMapper::getArray($id2); @@ -267,6 +274,7 @@ class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase self::assertEquals($modelR['float'], $modelR2['float']); self::assertEquals($modelR['null'], $modelR2['null']); self::assertEquals($modelR['datetime']->format('Y-m-d'), $modelR2['datetime']->format('Y-m-d')); + self::assertEquals(null, $modelR2['datetime_null']); // todo test update relations } diff --git a/tests/DataStorage/Database/TestModel/BaseModel.php b/tests/DataStorage/Database/TestModel/BaseModel.php index 0401c3551..f4e1fdc56 100644 --- a/tests/DataStorage/Database/TestModel/BaseModel.php +++ b/tests/DataStorage/Database/TestModel/BaseModel.php @@ -30,6 +30,8 @@ class BaseModel public $datetime = null; + public $datetime_null = null; + public $hasManyDirect = []; public $hasManyRelations = []; diff --git a/tests/DataStorage/Database/TestModel/BaseModelMapper.php b/tests/DataStorage/Database/TestModel/BaseModelMapper.php index 544ea3966..304353994 100644 --- a/tests/DataStorage/Database/TestModel/BaseModelMapper.php +++ b/tests/DataStorage/Database/TestModel/BaseModelMapper.php @@ -34,6 +34,7 @@ class BaseModelMapper extends DataMapperAbstract 'test_base_json' => ['name' => 'test_base_json', 'type' => 'Json', 'internal' => 'json'], 'test_base_json_serializable' => ['name' => 'test_base_json_serializable', 'type' => 'Json', 'internal' => 'jsonSerializable'], 'test_base_datetime' => ['name' => 'test_base_datetime', 'type' => 'DateTime', 'internal' => 'datetime'], + 'test_base_datetime_null' => ['name' => 'test_base_datetime_null', 'type' => 'DateTime', 'internal' => 'datetime_null'], 'test_base_owns_one_self' => ['name' => 'test_base_owns_one_self', 'type' => 'int', 'internal' => 'ownsOneSelf'], 'test_base_belongs_to_one' => ['name' => 'test_base_belongs_to_one', 'type' => 'int', 'internal' => 'belongsToOne'], ];