From 3a17d5c42747aaeece1d81a71af368473f7d6f9e Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Wed, 30 Mar 2011 14:19:27 +1300 Subject: [PATCH] API CHANGE: Add removeByID(), canSortBy(), and byID() to DataList and its subclasses. --- core/model/DataList.php | 25 ++++++++++++++++++++++- core/model/HasManyList.php | 15 +++++++++++--- core/model/ManyManyList.php | 19 ++++++++++++----- tests/model/DataExtensionTest.php | 2 +- tests/model/DataObjectTest.php | 34 ++++++++++++++++++++++++++++++- 5 files changed, 84 insertions(+), 11 deletions(-) diff --git a/core/model/DataList.php b/core/model/DataList.php index 51f491c8a..6d3dd7c67 100644 --- a/core/model/DataList.php +++ b/core/model/DataList.php @@ -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) { diff --git a/core/model/HasManyList.php b/core/model/HasManyList.php index 194d49443..87ec54842 100644 --- a/core/model/HasManyList.php +++ b/core/model/HasManyList.php @@ -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; diff --git a/core/model/ManyManyList.php b/core/model/ManyManyList.php index 5814644d2..60666bba4 100644 --- a/core/model/ManyManyList.php +++ b/core/model/ManyManyList.php @@ -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; diff --git a/tests/model/DataExtensionTest.php b/tests/model/DataExtensionTest.php index 0a8c7f4a8..d8f6fb0d6 100644 --- a/tests/model/DataExtensionTest.php +++ b/tests/model/DataExtensionTest.php @@ -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()); } diff --git a/tests/model/DataObjectTest.php b/tests/model/DataObjectTest.php index 3f028108f..49a1bd400 100755 --- a/tests/model/DataObjectTest.php +++ b/tests/model/DataObjectTest.php @@ -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 {