Permalink
Browse files

ENHANCEMENT Allowing to save ManyManyList with multiple foreign keys …

…(e.g. required to add to an overloaded Group->Members() relationship)
  • Loading branch information...
1 parent 8617859 commit 51bae9e4a6bdafcdee7d18d6e25cee37ad72f189 @chillu chillu committed Mar 6, 2012
Showing with 40 additions and 16 deletions.
  1. +1 −1 model/DataQuery.php
  2. +12 −13 model/ManyManyList.php
  3. +10 −0 model/RelationList.php
  4. +2 −1 tests/model/DataObjectTest.yml
  5. +15 −1 tests/model/ManyManyListTest.php
View
2 model/DataQuery.php
@@ -62,7 +62,7 @@ function removeFilterOn($fieldExpression) {
}
}
- if(!$matched) user_error("Couldn't find $fieldExpression in the query filter.", E_USER_WARNING);
+ if(!$matched) throw new InvalidArgumentException("Couldn't find $fieldExpression in the query filter.");
return $this;
}
View
25 model/ManyManyList.php
@@ -93,25 +93,24 @@ function add($item, $extraFields = null) {
if(!$this->foreignID) {
throw new Exception("ManyManyList::add() can't be called until a foreign ID is set", E_USER_WARNING);
}
- if(is_array($this->foreignID)) {
- throw new Exception("ManyManyList::add() can't be called on a list linked to mulitple foreign IDs", E_USER_WARNING);
- }
-
+
// Delete old entries, to prevent duplication
$this->removeById($itemID);
- // Insert new entry
- $manipulation = array();
- $manipulation[$this->joinTable]['command'] = 'insert';
+ // Insert new entry/entries
+ foreach((array)$this->foreignID as $foreignID) {
+ $manipulation = array();
+ $manipulation[$this->joinTable]['command'] = 'insert';
- if($extraFields) foreach($extraFields as $k => $v) {
- $manipulation[$this->joinTable]['fields'][$k] = "'" . Convert::raw2sql($v) . "'";
- }
+ if($extraFields) foreach($extraFields as $k => $v) {
+ $manipulation[$this->joinTable]['fields'][$k] = "'" . Convert::raw2sql($v) . "'";
+ }
- $manipulation[$this->joinTable]['fields'][$this->localKey] = $itemID;
- $manipulation[$this->joinTable]['fields'][$this->foreignKey] = $this->foreignID;
+ $manipulation[$this->joinTable]['fields'][$this->localKey] = $itemID;
+ $manipulation[$this->joinTable]['fields'][$this->foreignKey] = $foreignID;
- DB::manipulate($manipulation);
+ DB::manipulate($manipulation);
+ }
}
/**
View
10 model/RelationList.php
@@ -14,11 +14,21 @@
* @param $id A single ID, or an array of IDs
*/
function setForeignID($id) {
+ // If already filtered on foreign ID, remove that first
+ if($this->foreignID !== null) {
+ $oldFilter = $this->foreignIDFilter();
+ try {
+ $this->dataQuery->removeFilterOn($oldFilter);
+ } catch(InvalidArgumentException $e) {}
+ }
+
// Turn a 1-element array into a simple value
if(is_array($id) && sizeof($id) == 1) $id = reset($id);
$this->foreignID = $id;
$this->dataQuery->where($this->foreignIDFilter());
+
+ return $this;
}
/**
View
3 tests/model/DataObjectTest.yml
@@ -3,7 +3,8 @@ DataObjectTest_Team:
Title: Team 1
team2:
Title: Team 2
-
+ team3:
+ Title: Team 3
DataObjectTest_Player:
captain1:
FirstName: Captain
View
16 tests/model/ManyManyListTest.php
@@ -33,7 +33,7 @@ public function testAddingSingleDataObjectByReference() {
$compareTeams->byID($team1->ID);
$this->assertEquals($player1->Teams()->column('ID'),$compareTeams->column('ID'),"Adding single record as DataObject to many_many");
}
-
+
public function testRemovingSingleDataObjectByReference() {
$player1 = $this->objFromFixture('DataObjectTest_Player', 'player1');
$team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
@@ -78,6 +78,20 @@ public function testSetByIdList() {
$player1->Teams()->setByIdList(array($team2->ID));
$this->assertEquals(array($team2->ID), $player1->Teams()->column());
}
+
+ public function testAddingWithMultipleForeignKeys() {
+ $newPlayer = new DataObjectTest_Player();
+ $newPlayer->write();
+ $team1 = $this->objFromFixture('DataObjectTest_Team', 'team1');
+ $team2 = $this->objFromFixture('DataObjectTest_Team', 'team2');
+
+ $playersTeam1Team2 = DataList::create('DataObjectTest_Team')->relation('Players')->setForeignID(array($team1->ID, $team2->ID));
+ $playersTeam1Team2->add($newPlayer);
+ $this->assertEquals(
+ array($team1->ID, $team2->ID),
+ $newPlayer->Teams()->column('ID')
+ );
+ }
public function testSubtractOnAManyManyList() {
$allList = ManyManyList::create('DataObjectTest_Player', 'DataObjectTest_Team_Players','DataObjectTest_PlayerID', 'DataObjectTest_TeamID');

0 comments on commit 51bae9e

Please sign in to comment.