mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
API CHANGE: Add removeByID(), canSortBy(), and byID() to DataList and its subclasses.
This commit is contained in:
parent
b8f736d665
commit
3a17d5c427
@ -77,6 +77,13 @@ class DataList extends DataObjectSet {
|
|||||||
return $this;
|
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.
|
* Add an join clause to this data list's query.
|
||||||
*/
|
*/
|
||||||
@ -243,6 +250,14 @@ class DataList extends DataObjectSet {
|
|||||||
return $this;
|
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.
|
* Return a single column from this DataList.
|
||||||
* @param $colNum The DataObject field to return.
|
* @param $colNum The DataObject field to return.
|
||||||
@ -318,7 +333,7 @@ class DataList extends DataObjectSet {
|
|||||||
*/
|
*/
|
||||||
function removeMany($idList) {
|
function removeMany($idList) {
|
||||||
foreach($idList as $id) {
|
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
|
// Methods that won't function on DataLists
|
||||||
|
|
||||||
function push($item) {
|
function push($item) {
|
||||||
|
@ -60,12 +60,21 @@ class HasManyList extends RelationList {
|
|||||||
/**
|
/**
|
||||||
* Remove an item from this relation.
|
* Remove an item from this relation.
|
||||||
* Doesn't actually remove the item, it just clears the foreign key value.
|
* 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?
|
* @todo Maybe we should delete the object instead?
|
||||||
*/
|
*/
|
||||||
function remove($item) {
|
function remove($item) {
|
||||||
if(is_numeric($item)) $item = DataObject::get_by_id($this->dataClass, $item);
|
if(!($item instanceof $this->dataClass)) throw new InvalidArgumentException("HasManyList::remove() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
|
||||||
else if(!($item instanceof $this->dataClass)) user_eror("HasManyList::remove() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
|
|
||||||
|
|
||||||
$fk = $this->foreignKey;
|
$fk = $this->foreignKey;
|
||||||
$item->$fk = null;
|
$item->$fk = null;
|
||||||
|
@ -78,7 +78,7 @@ class ManyManyList extends RelationList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete old entries, to prevent duplication
|
// Delete old entries, to prevent duplication
|
||||||
$this->remove($itemID);
|
$this->removeById($itemID);
|
||||||
|
|
||||||
// Insert new entry
|
// Insert new entry
|
||||||
$manipulation = array();
|
$manipulation = array();
|
||||||
@ -97,12 +97,21 @@ class ManyManyList extends RelationList {
|
|||||||
/**
|
/**
|
||||||
* Remove the given item from this list.
|
* Remove the given item from this list.
|
||||||
* Note that for a ManyManyList, the item is never actually deleted, only the join table is affected
|
* 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) {
|
function remove($item) {
|
||||||
if(is_numeric($item)) $itemID = $item;
|
if(!($item instanceof $this->dataClass)) throw new InvalidArgumentException("ManyManyList::remove() expecting a $this->dataClass object");
|
||||||
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);
|
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 = new SQLQuery("*", array($this->joinTable));
|
||||||
$query->delete = true;
|
$query->delete = true;
|
||||||
|
@ -66,7 +66,7 @@ class DataExtensionTest extends SapphireTest {
|
|||||||
$parent->Faves()->add($obj2->ID);
|
$parent->Faves()->add($obj2->ID);
|
||||||
$this->assertEquals(2, $parent->Faves()->Count());
|
$this->assertEquals(2, $parent->Faves()->Count());
|
||||||
|
|
||||||
$parent->Faves()->remove($obj2->ID);
|
$parent->Faves()->removeByID($obj2->ID);
|
||||||
$this->assertEquals(1, $parent->Faves()->Count());
|
$this->assertEquals(1, $parent->Faves()->Count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -310,7 +310,7 @@ class DataObjectTest extends SapphireTest {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// test removing single DataObject by ID
|
// test removing single DataObject by ID
|
||||||
$player1->Teams()->remove($team1->ID);
|
$player1->Teams()->removeByID($team1->ID);
|
||||||
$player1->flushCache();
|
$player1->flushCache();
|
||||||
$compareTeams = new ComponentSet();
|
$compareTeams = new ComponentSet();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
@ -1067,6 +1067,38 @@ class DataObjectTest extends SapphireTest {
|
|||||||
$objEmpty->Title = '0'; //
|
$objEmpty->Title = '0'; //
|
||||||
$this->assertFalse($objEmpty->isEmpty(), 'Zero value in attribute considered non-empty');
|
$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 {
|
class DataObjectTest_Player extends Member implements TestOnly {
|
||||||
|
Loading…
Reference in New Issue
Block a user