From 76ae5bc38af677d0b814bf80d5aa1120ff86d448 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Fri, 8 Jan 2021 15:25:38 +1300 Subject: [PATCH] FIX Type checking in objectForKey() to fix postgres bug --- src/Forms/TreeDropdownField.php | 5 ++++- tests/php/Forms/TreeDropdownFieldTest.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Forms/TreeDropdownField.php b/src/Forms/TreeDropdownField.php index d51fc85f6..9ed9def62 100644 --- a/src/Forms/TreeDropdownField.php +++ b/src/Forms/TreeDropdownField.php @@ -812,10 +812,13 @@ class TreeDropdownField extends FormField * Get the object where the $keyField is equal to a certain value * * @param string|int $key - * @return DataObject + * @return DataObject|null */ protected function objectForKey($key) { + if (!is_string($key) && !is_int($key)) { + return null; + } return DataObject::get($this->getSourceObject()) ->filter($this->getKeyField(), $key) ->first(); diff --git a/tests/php/Forms/TreeDropdownFieldTest.php b/tests/php/Forms/TreeDropdownFieldTest.php index c06bc54d7..651dd5879 100644 --- a/tests/php/Forms/TreeDropdownFieldTest.php +++ b/tests/php/Forms/TreeDropdownFieldTest.php @@ -8,7 +8,10 @@ use SilverStripe\Control\Session; use SilverStripe\Dev\CSSContentParser; use SilverStripe\Dev\SapphireTest; use SilverStripe\Control\HTTPRequest; +use SilverStripe\Forms\FieldList; +use SilverStripe\Forms\Form; use SilverStripe\Forms\TreeDropdownField; +use SilverStripe\ORM\DataObject; use SilverStripe\ORM\Tests\HierarchyTest\TestObject; class TreeDropdownFieldTest extends SapphireTest @@ -246,4 +249,21 @@ class TreeDropdownFieldTest extends SapphireTest $result ); } + + /** + * This is to test setting $key to an Object in the protected function objectForKey() + * This is to fix an issue where postgres will not fail gracefully when you do this + */ + public function testObjectForKeyObjectValue() + { + $form = Form::create(); + $fieldList = FieldList::create(); + $field = TreeDropdownField::create('TestTree', 'Test tree', File::class); + $fieldList->add($field); + $form->setFields($fieldList); + $field->setValue(DataObject::create()); + // The following previously errored in postgres + $field->getSchemaStateDefaults(); + $this->assertTrue(true); + } }