Skip to content

Commit

Permalink
API CHANGE: Allow <classname>::get(), eg, Member::get()->byID(5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Minnee committed Apr 20, 2012
1 parent 33ae836 commit 8e48e6e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
17 changes: 16 additions & 1 deletion model/DataObject.php
Expand Up @@ -2488,7 +2488,22 @@ public function extendedSQL($filter = "", $sort = "", $limit = "", $join = ""){
*
* @return mixed The objects matching the filter, in the class specified by $containerClass
*/
public static function get($callerClass, $filter = "", $sort = "", $join = "", $limit = null, $containerClass = "DataList") {
public static function get($callerClass = null, $filter = "", $sort = "", $join = "", $limit = null, $containerClass = "DataList") {
if($callerClass == null) {
$callerClass = get_called_class();
if($callerClass == 'DataObject') {
throw new \InvalidArgumentException("Call <classname>::get() instead of DataObject::get()");
}

if($filter || $sort || $join || $limit || ($containerClass != "DataList")) {
throw new \InvalidArgumentException("If calling <classname>::get() then you shouldn't pass any other arguments");
}

$result = DataList::create(get_called_class());
$result->setModel(DataModel::inst());
return $result;
}

// Todo: Determine if we can deprecate for 3.0.0 and use DI or something instead
// Todo: Make the $containerClass method redundant
if($containerClass != "DataList") user_error("The DataObject::get() \$containerClass argument has been deprecated", E_USER_NOTICE);
Expand Down
18 changes: 18 additions & 0 deletions tests/model/DataObjectTest.php
Expand Up @@ -1036,6 +1036,24 @@ function testRelObject() {
$this->assertEquals("Team 1", $player->relObject('Teams.First.Title')->getValue());
}

function testLateStaticBindingStyle() {
// Confirm that DataObjectTest_Player::get() operates as excepted
$this->assertEquals(4, DataObjectTest_Player::get()->Count());
$this->assertInstanceOf('DataObjectTest_Player', DataObjectTest_Player::get()->First());

// You can't pass arguments to LSB syntax - use the DataList methods instead.
$this->setExpectedException('InvalidArgumentException');
DataObjectTest_Player::get(null, "\"ID\" = 1");

}

function testBrokenLateStaticBindingStyle() {
// If you call DataObject::get() you have to pass a first argument
$this->setExpectedException('InvalidArgumentException');
DataObject::get();

}


}

Expand Down

0 comments on commit 8e48e6e

Please sign in to comment.