FIX: DataObject::get_one() misses return null, not false

Fixes https://github.com/silverstripe/silverstripe-framework/issues/5441
This commit is contained in:
Sam Minnee 2017-06-29 12:26:40 +12:00
parent e7df10dc52
commit 2c8790ca7d
2 changed files with 17 additions and 2 deletions

View File

@ -2824,7 +2824,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @param boolean $cache Use caching
* @param string $orderby A sort expression to be inserted into the ORDER BY clause.
*
* @return DataObject The first item matching the query
* @return DataObject|null The first item matching the query
*/
public static function get_one($callerClass, $filter = "", $cache = true, $orderby = "")
{
@ -2851,7 +2851,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
}
}
}
return $cache ? self::$_cache_get_one[$callerClass][$cacheKey] : $item;
if ($cache) {
return self::$_cache_get_one[$callerClass][$cacheKey] ?: null;
} else {
return $item;
}
}
/**

View File

@ -2071,4 +2071,14 @@ class DataObjectTest extends SapphireTest
$staff->write();
$this->assertEquals(PHP_INT_MAX, DataObjectTest\Staff::get()->byID($staff->ID)->Salary);
}
public function testGetOneMissingValueReturnsNull()
{
// Test that missing values return null
$this->assertEquals(null, DataObject::get_one(
DataObjectTest\TeamComment::class,
['"DataObjectTest_TeamComment"."Name"' => 'does not exists']
));
}
}