mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #10460 from creative-commoners/pulls/5/rescue-master-dataobject-get-any-one
API Rescue Master Branch PR: Allow dataobject get_one without passing a class
This commit is contained in:
commit
f5d72e998c
@ -3399,24 +3399,34 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
|
|||||||
* $member = DataObject::get_one('Member', [ '"FirstName"' => 'John' ]);
|
* $member = DataObject::get_one('Member', [ '"FirstName"' => 'John' ]);
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
* @param string $callerClass The class of objects to be returned
|
* @param string|null $callerClass The class of objects to be returned. Defaults to the class that calls the method
|
||||||
|
* e.g. MyObject::get_one() will return a MyObject
|
||||||
* @param string|array $filter A filter to be inserted into the WHERE clause.
|
* @param string|array $filter A filter to be inserted into the WHERE clause.
|
||||||
* @param boolean $cache Use caching
|
* @param boolean $cache Use caching
|
||||||
* @param string $orderby A sort expression to be inserted into the ORDER BY clause.
|
* @param string $orderBy A sort expression to be inserted into the ORDER BY clause.
|
||||||
*
|
*
|
||||||
* @return DataObject|null 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 = "")
|
public static function get_one($callerClass = null, $filter = "", $cache = true, $orderBy = "")
|
||||||
{
|
{
|
||||||
|
if ($callerClass === null) {
|
||||||
|
$callerClass = static::class;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate class
|
||||||
|
if ($callerClass === self::class) {
|
||||||
|
throw new InvalidArgumentException('DataObject::get_one() cannot query non-subclass DataObject directly');
|
||||||
|
}
|
||||||
|
|
||||||
/** @var DataObject $singleton */
|
/** @var DataObject $singleton */
|
||||||
$singleton = singleton($callerClass);
|
$singleton = singleton($callerClass);
|
||||||
|
|
||||||
$cacheComponents = [$filter, $orderby, $singleton->getUniqueKeyComponents()];
|
$cacheComponents = [$filter, $orderBy, $singleton->getUniqueKeyComponents()];
|
||||||
$cacheKey = md5(serialize($cacheComponents));
|
$cacheKey = md5(serialize($cacheComponents));
|
||||||
|
|
||||||
$item = null;
|
$item = null;
|
||||||
if (!$cache || !isset(self::$_cache_get_one[$callerClass][$cacheKey])) {
|
if (!$cache || !isset(self::$_cache_get_one[$callerClass][$cacheKey])) {
|
||||||
$dl = DataObject::get($callerClass)->where($filter)->sort($orderby);
|
$dl = DataObject::get($callerClass)->where($filter)->sort($orderBy);
|
||||||
$item = $dl->first();
|
$item = $dl->first();
|
||||||
|
|
||||||
if ($cache) {
|
if ($cache) {
|
||||||
|
@ -436,6 +436,12 @@ class DataObjectTest extends SapphireTest
|
|||||||
$this->assertEquals('Bob', $comment->Name);
|
$this->assertEquals('Bob', $comment->Name);
|
||||||
$comment = DataObject::get_one(DataObjectTest\TeamComment::class, '', true, '"Name" DESC');
|
$comment = DataObject::get_one(DataObjectTest\TeamComment::class, '', true, '"Name" DESC');
|
||||||
$this->assertEquals('Phil', $comment->Name);
|
$this->assertEquals('Phil', $comment->Name);
|
||||||
|
|
||||||
|
// Test get_one() without passing classname
|
||||||
|
$this->assertEquals(
|
||||||
|
DataObjectTest\TeamComment::get_one(),
|
||||||
|
DataObject::get_one(DataObjectTest\TeamComment::class)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetByIDCallerClass()
|
public function testGetByIDCallerClass()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user