Skip to content

Commit 8672924

Browse files
committed
Fix for nullable fields on new models
1 parent d8d49d5 commit 8672924

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/Models/ActiveRecord.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,11 @@ protected function _setFieldValue($field, $value)
16481648
}
16491649
}
16501650

1651-
if ($forceDirty || (empty($this->_record[$field]) && isset($value)) || ($this->_record[$field] !== $value)) {
1651+
$columnName = static::_cn($field);
1652+
$recordHasValue = array_key_exists($columnName, $this->_record);
1653+
$currentValue = $recordHasValue ? $this->_record[$columnName] : null;
1654+
1655+
if ($forceDirty || (!$recordHasValue && isset($value)) || ($recordHasValue && $currentValue !== $value)) {
16521656
$this->_setValueAndMarkDirty($field, $value, $fieldOptions);
16531657
return true;
16541658
} else {

tests/Divergence/Models/ActiveRecordTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,17 @@ public function testSaveCanaryTimestamps()
715715
$this->assertNull($x->DateOfBirth);
716716
}
717717

718+
public function testNewCanaryAllowsNullableFieldsToBeSetWithoutWarnings()
719+
{
720+
$Canary = new Canary();
721+
722+
$Canary->Handle = null;
723+
$Canary->LongestFlightTime = null;
724+
725+
$this->assertNull($Canary->Handle);
726+
$this->assertNull($Canary->LongestFlightTime);
727+
}
728+
718729
/**
719730
*
720731
*

0 commit comments

Comments
 (0)