Skip to content

Commit

Permalink
FIX Type checking in objectForKey() to fix postgres bug
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 8, 2021
1 parent 8438e74 commit 76ae5bc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Forms/TreeDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,10 +812,13 @@ protected function getSearchResults()
* 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();
Expand Down
20 changes: 20 additions & 0 deletions tests/php/Forms/TreeDropdownFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
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
Expand Down Expand Up @@ -246,4 +249,21 @@ public function testReadonly()
$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);
}
}

0 comments on commit 76ae5bc

Please sign in to comment.