Skip to content
This repository
Browse code

API Prep forForeignID for immutability in 3.1 per 7673

  • Loading branch information...
commit b7691077cff314108df3e9af6fa729771f707c27 1 parent e8e4604
Hamish Friedlander authored July 23, 2012
4  model/DataObject.php
@@ -1288,7 +1288,7 @@ public function getComponents($componentName, $filter = "", $sort = "", $join =
1288 1288
 		
1289 1289
 		$result = new HasManyList($componentClass, $joinField);
1290 1290
 		if($this->model) $result->setDataModel($this->model);
1291  
-		$result->setForeignID($this->ID);
  1291
+		$result = $result->forForeignID($this->ID);
1292 1292
 
1293 1293
 		$result = $result->where($filter)->limit($limit)->sort($sort);
1294 1294
 		if($join) $result = $result->join($join);
@@ -1412,7 +1412,7 @@ public function getManyManyComponents($componentName, $filter = "", $sort = "",
1412 1412
 
1413 1413
 		// If this is called on a singleton, then we return an 'orphaned relation' that can have the
1414 1414
 		// foreignID set elsewhere.
1415  
-		$result->setForeignID($this->ID);
  1415
+		$result = $result->forForeignID($this->ID);
1416 1416
 			
1417 1417
 		return $result->where($filter)->sort($sort)->limit($limit);
1418 1418
 	}
14  model/RelationList.php
@@ -11,6 +11,10 @@
11 11
 	
12 12
 	/**
13 13
 	 * Set the ID of the record that this ManyManyList is linking *from*.
  14
+	 *
  15
+	 * This is the mutatable version of this function, and will be protected only
  16
+	 * from 3.1. Use forForeignID instead
  17
+	 *
14 18
 	 * @param $id A single ID, or an array of IDs
15 19
 	 */
16 20
 	function setForeignID($id) {
@@ -19,7 +23,8 @@ function setForeignID($id) {
19 23
 			$oldFilter = $this->foreignIDFilter();
20 24
 			try {
21 25
 				$this->dataQuery->removeFilterOn($oldFilter);	
22  
-			} catch(InvalidArgumentException $e) {}
  26
+			}
  27
+			catch(InvalidArgumentException $e) { /* NOP */ }
23 28
 		}
24 29
 
25 30
 		// Turn a 1-element array into a simple value
@@ -32,12 +37,13 @@ function setForeignID($id) {
32 37
 	}
33 38
 	
34 39
 	/**
35  
-	 * Returns this ManyMany relationship linked to the given foreign ID.
  40
+	 * Returns a copy of this list with the ManyMany relationship linked to the given foreign ID.
36 41
 	 * @param $id An ID or an array of IDs.
37 42
 	 */
38 43
 	function forForeignID($id) {
39  
-		$this->setForeignID($id);
40  
-		return $this;
  44
+		return $this->alterDataQuery_30(function($query, $list) use ($id){
  45
+			$list->setForeignID($id);
  46
+		});
41 47
 	}
42 48
 	
43 49
 	abstract protected function foreignIDFilter();
2  security/Member.php
@@ -935,7 +935,7 @@ public function getTimeFormat() {
935 935
 	 */
936 936
 	public function Groups() {
937 937
 		$groups = Injector::inst()->create('Member_GroupSet', 'Group', 'Group_Members', 'GroupID', 'MemberID');
938  
-		$groups->setForeignID($this->ID);
  938
+		$groups = $groups->forForeignID($this->ID);
939 939
 		
940 940
 		$this->extend('updateGroups', $groups);
941 941
 
14  tests/model/ManyManyListTest.php
@@ -29,8 +29,7 @@ public function testAddingSingleDataObjectByReference() {
29 29
 		$player1->flushCache();
30 30
 
31 31
 		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
32  
-		$compareTeams->forForeignID($player1->ID);
33  
-		$compareTeams->byID($team1->ID);
  32
+		$compareTeams = $compareTeams->forForeignID($player1->ID);
34 33
 		$this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),"Adding single record as DataObject to many_many");
35 34
 	}
36 35
 
@@ -40,8 +39,7 @@ public function testRemovingSingleDataObjectByReference() {
40 39
 		$player1->Teams()->remove($team1);
41 40
 		$player1->flushCache();
42 41
 		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
43  
-		$compareTeams->forForeignID($player1->ID);
44  
-		$compareTeams->byID($team1->ID);
  42
+		$compareTeams = $compareTeams->forForeignID($player1->ID);
45 43
 		$this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),"Removing single record as DataObject from many_many");
46 44
 	}
47 45
 	
@@ -51,8 +49,7 @@ public function testAddingSingleDataObjectByID() {
51 49
 		$player1->Teams()->add($team1->ID);
52 50
 		$player1->flushCache();
53 51
 		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
54  
-		$compareTeams->forForeignID($player1->ID);
55  
-		$compareTeams->byID($team1->ID);
  52
+		$compareTeams = $compareTeams->forForeignID($player1->ID);
56 53
 		$this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'), "Adding single record as ID to many_many");
57 54
 	}
58 55
 	
@@ -62,8 +59,7 @@ public function testRemoveByID() {
62 59
 		$player1->Teams()->removeByID($team1->ID);
63 60
 		$player1->flushCache();
64 61
 		$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
65  
-		$compareTeams->forForeignID($player1->ID);
66  
-		$compareTeams->byID($team1->ID);
  62
+		$compareTeams = $compareTeams->forForeignID($player1->ID);
67 63
 		$this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'), "Removing single record as ID from many_many");
68 64
 	}
69 65
 	
@@ -85,7 +81,7 @@ public function testAddingWithMultipleForeignKeys() {
85 81
 		$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
86 82
 		$team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
87 83
 
88  
-		$playersTeam1Team2 = DataObjectTest_Team::get()->relation('Players')->setForeignID(array($team1->ID, $team2->ID));
  84
+		$playersTeam1Team2 = DataObjectTest_Team::get()->relation('Players')->forForeignID(array($team1->ID, $team2->ID));
89 85
 		$playersTeam1Team2->add($newPlayer);
90 86
 		$this->assertEquals(
91 87
 			array($team1->ID, $team2->ID),

0 notes on commit b769107

Please sign in to comment.
Something went wrong with that request. Please try again.