API CHANGE: Add removeByID(), canSortBy(), and byID() to DataList and its subclasses.

This commit is contained in:
Sam Minnee 2011-03-30 14:19:27 +13:00
parent b8f736d665
commit 3a17d5c427
5 changed files with 84 additions and 11 deletions

View File

@ -76,6 +76,13 @@ class DataList extends DataObjectSet {
$this->dataQuery->sort($sort);
return $this;
}
/**
* Returns true if this DataList can be sorted by the given field.
*/
public function canSortBy($field) {
return $this->dataQuery()->query()->canSortBy($field);
}
/**
* Add an join clause to this data list's query.
@ -242,6 +249,14 @@ class DataList extends DataObjectSet {
return $this;
}
/**
* Return the item of the given ID
*/
public function byID($id) {
$baseClass = ClassInfo::baseDataClass($this->dataClass);
return $this->filter("\"$baseClass\".\"ID\" = " . (int)$id)->First();
}
/**
* Return a single column from this DataList.
@ -318,7 +333,7 @@ class DataList extends DataObjectSet {
*/
function removeMany($idList) {
foreach($idList as $id) {
$this->remove($id);
$this->removeByID($id);
}
}
@ -357,6 +372,14 @@ class DataList extends DataObjectSet {
}
/**
* Remove an item from this DataList by ID
*/
function removeByID($itemID) {
$item = $this->byID($itemID);
if($item) return $item->delete();
}
// Methods that won't function on DataLists
function push($item) {

View File

@ -60,12 +60,21 @@ class HasManyList extends RelationList {
/**
* Remove an item from this relation.
* Doesn't actually remove the item, it just clears the foreign key value.
* @param $item The DataObject to be removed, or its ID
* @param $itemID The ID of the item to be removed
*/
function removeByID($itemID) {
$item = $this->byID($item);
return $this->remove($item);
}
/**
* Remove an item from this relation.
* Doesn't actually remove the item, it just clears the foreign key value.
* @param $item The DataObject to be removed
* @todo Maybe we should delete the object instead?
*/
function remove($item) {
if(is_numeric($item)) $item = DataObject::get_by_id($this->dataClass, $item);
else if(!($item instanceof $this->dataClass)) user_eror("HasManyList::remove() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
if(!($item instanceof $this->dataClass)) throw new InvalidArgumentException("HasManyList::remove() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
$fk = $this->foreignKey;
$item->$fk = null;

View File

@ -78,7 +78,7 @@ class ManyManyList extends RelationList {
}
// Delete old entries, to prevent duplication
$this->remove($itemID);
$this->removeById($itemID);
// Insert new entry
$manipulation = array();
@ -97,12 +97,21 @@ class ManyManyList extends RelationList {
/**
* Remove the given item from this list.
* Note that for a ManyManyList, the item is never actually deleted, only the join table is affected
* @param $item The data object or its ID
* @param $itemID The ID of the item to remove.
*/
function remove($item) {
if(is_numeric($item)) $itemID = $item;
else if($item instanceof $this->dataClass) $itemID = $item->ID;
else user_eror("ManyManyList::remove() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
if(!($item instanceof $this->dataClass)) throw new InvalidArgumentException("ManyManyList::remove() expecting a $this->dataClass object");
return $this->removeByID($item->ID);
}
/**
* Remove the given item from this list.
* Note that for a ManyManyList, the item is never actually deleted, only the join table is affected
* @param $itemID The item it
*/
function removeByID($itemID) {
if(!is_numeric($itemID)) throw new InvalidArgumentException("ManyManyList::removeById() expecting an ID");
$query = new SQLQuery("*", array($this->joinTable));
$query->delete = true;

View File

@ -66,7 +66,7 @@ class DataExtensionTest extends SapphireTest {
$parent->Faves()->add($obj2->ID);
$this->assertEquals(2, $parent->Faves()->Count());
$parent->Faves()->remove($obj2->ID);
$parent->Faves()->removeByID($obj2->ID);
$this->assertEquals(1, $parent->Faves()->Count());
}

View File

@ -310,7 +310,7 @@ class DataObjectTest extends SapphireTest {
);
// test removing single DataObject by ID
$player1->Teams()->remove($team1->ID);
$player1->Teams()->removeByID($team1->ID);
$player1->flushCache();
$compareTeams = new ComponentSet();
$this->assertEquals(
@ -1067,6 +1067,38 @@ class DataObjectTest extends SapphireTest {
$objEmpty->Title = '0'; //
$this->assertFalse($objEmpty->isEmpty(), 'Zero value in attribute considered non-empty');
}
/**
* Test DataList->byID()
*/
function testByID() {
$id = $this->idFromFixture('DataObjectTest_Team','team2');
$this->assertEquals('Team 2', DataObject::get("DataObjectTest_Team")->byID($id)->Title);
}
/**
* Test DataList->removeByID()
*/
function testRemoveByID() {
$id = $this->idFromFixture('DataObjectTest_Team','team2');
DataObject::get("DataObjectTest_Team")->removeByID($id);
$this->assertNull(DataObject::get("DataObjectTest_Team")->byID($id));
}
/**
* Test DataList->canSortBy()
*/
function testCanSortBy() {
// Basic check
$this->assertTrue(DataObject::get("DataObjectTest_Team")->canSortBy("Title"));
$this->assertFalse(DataObject::get("DataObjectTest_Team")->canSortBy("SomethingElse"));
// Subclasses
$this->assertTrue(DataObject::get("DataObjectTest_SubTeam")->canSortBy("Title"));
$this->assertTrue(DataObject::get("DataObjectTest_SubTeam")->canSortBy("SubclassDatabaseField"));
}
}
class DataObjectTest_Player extends Member implements TestOnly {