diff --git a/.run/DBD-PHP-Entity.run.xml b/.run/DBD-PHP-Entity.run.xml index f911ab7..798fb9a 100644 --- a/.run/DBD-PHP-Entity.run.xml +++ b/.run/DBD-PHP-Entity.run.xml @@ -1,5 +1,10 @@ + + + + diff --git a/composer.json b/composer.json index 5b331e3..837b69b 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "falseclock/dbd-php-entity", "type": "library", + "version": "3.0.0", "description": "DTO like library to fetch any data in an Object-Oriented manner (ORM)", "keywords": [ "dbd", @@ -23,7 +24,7 @@ } ], "require": { - "php": "^7.1|^8.0", + "php": "^8.0", "ext-json": "*", "myclabs/php-enum": "^1.7.7" }, diff --git a/src/DBD/Entity/Entity.php b/src/DBD/Entity/Entity.php index 7b402da..788d74d 100644 --- a/src/DBD/Entity/Entity.php +++ b/src/DBD/Entity/Entity.php @@ -212,6 +212,7 @@ private function setModelData(Mapper $map, int $maxLevels, int $currentLevel): v * @param Mapper $mapper * * @throws EntityException + * @throws \ReflectionException */ private function setBaseColumns(Mapper $mapper) { @@ -284,10 +285,25 @@ private function setBaseColumns(Mapper $mapper) * Entity public variables should not have default values. * But sometimes we need to have default value for column in case of $rowData has null value * In this case we should not override default value if $columnValue is null + * Иными словами нельзя переписывать дефолтное значение, если из базы пришло null + * но, если нет дефолтного значения, то мы должны его проинизиализировать null значением */ - if (!isset($this->$property) and isset($columnValue)) { - $this->$property = &$columnValue; - } + $reflection = new ReflectionObject($this); + $reflectionProperty = $reflection->getProperty($property); + + // Если мы еще не инциализировали переменную и у нас есть значение для этой переменной + //if (!isset($this->$property)) { + + // Если у нас есть значение, то ставим его + if (isset($columnValue)) { + $this->$property = &$columnValue; + } else { + // У нас нет прицепленного значения + if (!$reflectionProperty->hasDefaultValue()) { + $this->$property = $columnValue; // this is NULL value + } + } + //} } } } diff --git a/tests/DBD/Entity/Tests/Entities/EntityWithDefaults.php b/tests/DBD/Entity/Tests/Entities/EntityWithDefaults.php new file mode 100644 index 0000000..2147ac6 --- /dev/null +++ b/tests/DBD/Entity/Tests/Entities/EntityWithDefaults.php @@ -0,0 +1,35 @@ + "prefilled", + Column::PRIMITIVE_TYPE => StringPrimitives::String, + Column::NULLABLE => true + ]; + + public $unfiled = [ + Column::NAME => "unfilled", + Column::PRIMITIVE_TYPE => StringPrimitives::String, + Column::NULLABLE => true + ]; +} diff --git a/tests/DBD/Entity/Tests/EntityTest.php b/tests/DBD/Entity/Tests/EntityTest.php index 51ee65d..bc14d64 100644 --- a/tests/DBD/Entity/Tests/EntityTest.php +++ b/tests/DBD/Entity/Tests/EntityTest.php @@ -33,6 +33,7 @@ use DBD\Entity\Tests\Entities\DeclarationChain\C; use DBD\Entity\Tests\Entities\DeclarationChain\D; use DBD\Entity\Tests\Entities\DeclarationChain\E; +use DBD\Entity\Tests\Entities\EntityWithDefaults; use DBD\Entity\Tests\Entities\JsonTypeColumn; use DBD\Entity\Tests\Entities\JsonTypeColumnMap; use DBD\Entity\Tests\Entities\PersonBase; @@ -65,6 +66,26 @@ public function testRaw() self::assertSame($personData, $person->raw()); } + public function testPrefilledWithNullValues() { + $data = [ + 'prefilled' => null, + 'unfilled' => null, + ]; + + $entity = new EntityWithDefaults($data); + self::assertSame(EntityWithDefaults::PREFILL, $entity->prefiled); + self::assertSame(null, $entity->unfiled); + + $data = [ + 'prefilled' => "123", + 'unfilled' => "123", + ]; + + $entity = new EntityWithDefaults($data); + self::assertSame("123", $entity->prefiled); + self::assertSame("123", $entity->unfiled); + } + /** * @throws EntityException */