Skip to content

Commit 92609a6

Browse files
committed
Fixed nullable default values
1 parent e884702 commit 92609a6

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Tests/Unit/InsertTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function testDateNullInsert() : void
1717
$this->assertEquals($date, $insertedTest->dateRequired, 'dateRequired does not equal ' . $date);
1818
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNullable);
1919
$this->assertEquals('2000-01-02', $insertedTest->dateDefaultNotNull);
20-
$this->assertNull($insertedTest->timestampDefaultCurrentNullable, 'timestampDefaultCurrentNullable is not null');
20+
$this->assertGreaterThanOrEqual($timeStamp, $insertedTest->timestampDefaultCurrentNullable, 'timestampDefaultCurrentNullable is greater than ' . $timeStamp);
2121
$this->assertGreaterThanOrEqual($timeStamp, $insertedTest->timestampDefaultCurrentNotNull, 'timestampDefaultCurrentNotNull is wrong');
2222

2323
$this->assertTrue($transaction->rollBack());

src/PHPFUI/ORM/Record.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ public function __get(string $field) : mixed
123123
return $relationshipObject->getValue($relationship);
124124
}
125125

126+
if (isset(static::$fields[$field]))
127+
{
128+
return $this->current[$field] ?? null;
129+
}
130+
126131
return parent::__get($field);
127132
}
128133

@@ -480,7 +485,18 @@ public function setEmpty() : static
480485

481486
foreach (static::$fields as $field => $description)
482487
{
483-
$this->current[$field] = \in_array($description->defaultValue, self::$sqlDefaults) ? null : $description->defaultValue;
488+
if (null === $description->defaultValue) // no default value
489+
{
490+
if ($description->nullable) // can be null, so don't set
491+
{
492+
continue;
493+
}
494+
$this->current[$field] = null; // can't be null, so we can set to null, user must set
495+
}
496+
else // has default value, if SQL default, set to null, otherwise default value
497+
{
498+
$this->current[$field] = \in_array($description->defaultValue, self::$sqlDefaults) ? null : $description->defaultValue;
499+
}
484500
}
485501

486502
$this->correctTypes();
@@ -699,7 +715,7 @@ protected function correctTypes() : static
699715
{
700716
$relationshipClass = \array_shift($relationship);
701717
$relationshipObject = new $relationshipClass($this, $field);
702-
$relationshipObject->setValue($relationshipObject->fromPHPValue($this->current[$field], $relationship), $relationship);
718+
$relationshipObject->setValue($relationshipObject->fromPHPValue($this->current[$field] ?? null, $relationship), $relationship);
703719
}
704720
elseif (\array_key_exists($field, $this->current))
705721
{
@@ -831,10 +847,11 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
831847
{
832848
$definition = static::$fields[$key];
833849

834-
if (! $definition->nullable && null === $value && \in_array($definition->defaultValue, self::$sqlDefaults))
850+
if (null == $value && null != $definition->defaultValue)
835851
{
836852
continue;
837853
}
854+
// && \in_array($definition->defaultValue, self::$sqlDefaults))
838855

839856
if (! static::$autoIncrement || ! (\in_array($key, static::$primaryKeys) && empty($value)))
840857
{

0 commit comments

Comments
 (0)