Skip to content

Commit

Permalink
Merge pull request #8480 from andrewandante/pulls/5/allow_implied_dat…
Browse files Browse the repository at this point in the history
…aobject_get_one_class

allow dataobject get_one without passing a class
  • Loading branch information
robbieaverill committed Dec 12, 2018
2 parents 26bc923 + ddf2f49 commit 1f39084
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3102,24 +3102,33 @@ public static function get(
* Return the first item matching the given query.
* All calls to get_one() are cached.
*
* @param string $callerClass The class of objects to be returned
* @param string|array $filter A filter to be inserted into the WHERE clause.
* Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
* @param boolean $cache Use caching
* @param string $orderby A sort expression to be inserted into the ORDER BY clause.
* @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.
* Supports parameterised queries. See SQLSelect::addWhere() for syntax examples.
* @param boolean $cache Use caching
* @param string $orderBy A sort expression to be inserted into the ORDER BY clause.
*
* @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');
}
$SNG = singleton($callerClass);

$cacheComponents = array($filter, $orderby, $SNG->extend('cacheKeyComponent'));
$cacheComponents = array($filter, $orderBy, $SNG->extend('cacheKeyComponent'));
$cacheKey = md5(serialize($cacheComponents));

$item = null;
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();

if ($cache) {
Expand Down
6 changes: 6 additions & 0 deletions tests/php/ORM/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ public function testGet()
$this->assertEquals('Bob', $comment->Name);
$comment = DataObject::get_one(DataObjectTest\TeamComment::class, '', true, '"Name" DESC');
$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()
Expand Down

0 comments on commit 1f39084

Please sign in to comment.