API Prep forForeignID for immutability in 3.1 per 7673

This commit is contained in:
Hamish Friedlander 2012-07-23 10:31:47 +12:00
parent e8e4604457
commit b7691077cf
4 changed files with 18 additions and 16 deletions

View File

@ -1288,7 +1288,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$result = new HasManyList($componentClass, $joinField); $result = new HasManyList($componentClass, $joinField);
if($this->model) $result->setDataModel($this->model); if($this->model) $result->setDataModel($this->model);
$result->setForeignID($this->ID); $result = $result->forForeignID($this->ID);
$result = $result->where($filter)->limit($limit)->sort($sort); $result = $result->where($filter)->limit($limit)->sort($sort);
if($join) $result = $result->join($join); 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 // If this is called on a singleton, then we return an 'orphaned relation' that can have the
// foreignID set elsewhere. // foreignID set elsewhere.
$result->setForeignID($this->ID); $result = $result->forForeignID($this->ID);
return $result->where($filter)->sort($sort)->limit($limit); return $result->where($filter)->sort($sort)->limit($limit);
} }

View File

@ -11,6 +11,10 @@ abstract class RelationList extends DataList {
/** /**
* Set the ID of the record that this ManyManyList is linking *from*. * 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 * @param $id A single ID, or an array of IDs
*/ */
function setForeignID($id) { function setForeignID($id) {
@ -19,7 +23,8 @@ abstract class RelationList extends DataList {
$oldFilter = $this->foreignIDFilter(); $oldFilter = $this->foreignIDFilter();
try { try {
$this->dataQuery->removeFilterOn($oldFilter); $this->dataQuery->removeFilterOn($oldFilter);
} catch(InvalidArgumentException $e) {} }
catch(InvalidArgumentException $e) { /* NOP */ }
} }
// Turn a 1-element array into a simple value // 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. * @param $id An ID or an array of IDs.
*/ */
function forForeignID($id) { function forForeignID($id) {
$this->setForeignID($id); return $this->alterDataQuery_30(function($query, $list) use ($id){
return $this; $list->setForeignID($id);
});
} }
abstract protected function foreignIDFilter(); abstract protected function foreignIDFilter();

View File

@ -935,7 +935,7 @@ class Member extends DataObject implements TemplateGlobalProvider {
*/ */
public function Groups() { public function Groups() {
$groups = Injector::inst()->create('Member_GroupSet', 'Group', 'Group_Members', 'GroupID', 'MemberID'); $groups = Injector::inst()->create('Member_GroupSet', 'Group', 'Group_Members', 'GroupID', 'MemberID');
$groups->setForeignID($this->ID); $groups = $groups->forForeignID($this->ID);
$this->extend('updateGroups', $groups); $this->extend('updateGroups', $groups);

View File

@ -29,8 +29,7 @@ class ManyManyListTest extends SapphireTest {
$player1->flushCache(); $player1->flushCache();
$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
$compareTeams->forForeignID($player1->ID); $compareTeams = $compareTeams->forForeignID($player1->ID);
$compareTeams->byID($team1->ID);
$this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),"Adding single record as DataObject to many_many"); $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->Teams()->remove($team1);
$player1->flushCache(); $player1->flushCache();
$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
$compareTeams->forForeignID($player1->ID); $compareTeams = $compareTeams->forForeignID($player1->ID);
$compareTeams->byID($team1->ID);
$this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),"Removing single record as DataObject from many_many"); $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->Teams()->add($team1->ID);
$player1->flushCache(); $player1->flushCache();
$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
$compareTeams->forForeignID($player1->ID); $compareTeams = $compareTeams->forForeignID($player1->ID);
$compareTeams->byID($team1->ID);
$this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'), "Adding single record as ID to many_many"); $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->Teams()->removeByID($team1->ID);
$player1->flushCache(); $player1->flushCache();
$compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID'); $compareTeams = new ManyManyList('DataObjectTest_Team','DataObjectTest_Team_Players', 'DataObjectTest_TeamID', 'DataObjectTest_PlayerID');
$compareTeams->forForeignID($player1->ID); $compareTeams = $compareTeams->forForeignID($player1->ID);
$compareTeams->byID($team1->ID);
$this->assertEquals($player1->Teams()->column('ID'), $compareTeams->column('ID'), "Removing single record as ID from many_many"); $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'); $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
$team2 = $this->objFromFixture('DataObjectTest_Team', 'team2'); $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); $playersTeam1Team2->add($newPlayer);
$this->assertEquals( $this->assertEquals(
array($team1->ID, $team2->ID), array($team1->ID, $team2->ID),