From b7691077cff314108df3e9af6fa729771f707c27 Mon Sep 17 00:00:00 2001 From: Hamish Friedlander Date: Mon, 23 Jul 2012 10:31:47 +1200 Subject: [PATCH] API Prep forForeignID for immutability in 3.1 per 7673 --- model/DataObject.php | 4 ++-- model/RelationList.php | 14 ++++++++++---- security/Member.php | 2 +- tests/model/ManyManyListTest.php | 14 +++++--------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/model/DataObject.php b/model/DataObject.php index 3f10d6416..8f2885b9a 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -1288,7 +1288,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity $result = new HasManyList($componentClass, $joinField); if($this->model) $result->setDataModel($this->model); - $result->setForeignID($this->ID); + $result = $result->forForeignID($this->ID); $result = $result->where($filter)->limit($limit)->sort($sort); if($join) $result = $result->join($join); @@ -1412,7 +1412,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity // If this is called on a singleton, then we return an 'orphaned relation' that can have the // foreignID set elsewhere. - $result->setForeignID($this->ID); + $result = $result->forForeignID($this->ID); return $result->where($filter)->sort($sort)->limit($limit); } diff --git a/model/RelationList.php b/model/RelationList.php index 5ebe8db36..c78a3a6f1 100644 --- a/model/RelationList.php +++ b/model/RelationList.php @@ -11,6 +11,10 @@ abstract class RelationList extends DataList { /** * Set the ID of the record that this ManyManyList is linking *from*. + * + * This is the mutatable version of this function, and will be protected only + * from 3.1. Use forForeignID instead + * * @param $id A single ID, or an array of IDs */ function setForeignID($id) { @@ -19,7 +23,8 @@ abstract class RelationList extends DataList { $oldFilter = $this->foreignIDFilter(); try { $this->dataQuery->removeFilterOn($oldFilter); - } catch(InvalidArgumentException $e) {} + } + catch(InvalidArgumentException $e) { /* NOP */ } } // Turn a 1-element array into a simple value @@ -32,12 +37,13 @@ abstract class RelationList extends DataList { } /** - * Returns this ManyMany relationship linked to the given foreign ID. + * Returns a copy of this list with the ManyMany relationship linked to the given foreign ID. * @param $id An ID or an array of IDs. */ function forForeignID($id) { - $this->setForeignID($id); - return $this; + return $this->alterDataQuery_30(function($query, $list) use ($id){ + $list->setForeignID($id); + }); } abstract protected function foreignIDFilter(); diff --git a/security/Member.php b/security/Member.php index c885706ec..a534391c6 100644 --- a/security/Member.php +++ b/security/Member.php @@ -935,7 +935,7 @@ class Member extends DataObject implements TemplateGlobalProvider { */ public function Groups() { $groups = Injector::inst()->create('Member_GroupSet', 'Group', 'Group_Members', 'GroupID', 'MemberID'); - $groups->setForeignID($this->ID); + $groups = $groups->forForeignID($this->ID); $this->extend('updateGroups', $groups); diff --git a/tests/model/ManyManyListTest.php b/tests/model/ManyManyListTest.php index cb6f549a5..b229e5207 100644 --- a/tests/model/ManyManyListTest.php +++ b/tests/model/ManyManyListTest.php @@ -29,8 +29,7 @@ class ManyManyListTest extends SapphireTest { $player1->flushCache(); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); - $compareTeams->forForeignID($player1->ID); - $compareTeams->byID($team1->ID); + $compareTeams = $compareTeams->forForeignID($player1->ID); $this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),"Adding single record as DataObject to many_many"); } @@ -40,8 +39,7 @@ class ManyManyListTest extends SapphireTest { $player1->Teams()->remove($team1); $player1->flushCache(); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); - $compareTeams->forForeignID($player1->ID); - $compareTeams->byID($team1->ID); + $compareTeams = $compareTeams->forForeignID($player1->ID); $this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),"Removing single record as DataObject from many_many"); } @@ -51,8 +49,7 @@ class ManyManyListTest extends SapphireTest { $player1->Teams()->add($team1->ID); $player1->flushCache(); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); - $compareTeams->forForeignID($player1->ID); - $compareTeams->byID($team1->ID); + $compareTeams = $compareTeams->forForeignID($player1->ID); $this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'), "Adding single record as ID to many_many"); } @@ -62,8 +59,7 @@ class ManyManyListTest extends SapphireTest { $player1->Teams()->removeByID($team1->ID); $player1->flushCache(); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); - $compareTeams->forForeignID($player1->ID); - $compareTeams->byID($team1->ID); + $compareTeams = $compareTeams->forForeignID($player1->ID); $this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'), "Removing single record as ID from many_many"); } @@ -85,7 +81,7 @@ class ManyManyListTest extends SapphireTest { $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1'); $team2 = $this->objFromFixture('DataObjectTest_Team', 'team2'); - $playersTeam1Team2 = DataObjectTest_Team::get()->relation('Players')->setForeignID(array($team1->ID, $team2->ID)); + $playersTeam1Team2 = DataObjectTest_Team::get()->relation('Players')->forForeignID(array($team1->ID, $team2->ID)); $playersTeam1Team2->add($newPlayer); $this->assertEquals( array($team1->ID, $team2->ID),