/**
  * Returns a many-to-many component, as a ManyManyList.
  * @param string $componentName Name of the many-many component
  * @return ManyManyList The set of components
  */
 public function getManyManyComponents($componentName)
 {
     $manyManyComponent = $this->manyManyComponent($componentName);
     if (!$manyManyComponent) {
         throw new InvalidArgumentException(sprintf("DataObject::getComponents(): Unknown many-to-many component '%s' on class '%s'", $componentName, $this->class));
     }
     list($parentClass, $componentClass, $parentField, $componentField, $table) = $manyManyComponent;
     // If we haven't been written yet, we can't save these relations, so use a list that handles this case
     if (!$this->ID) {
         if (!isset($this->unsavedRelations[$componentName])) {
             $this->unsavedRelations[$componentName] = new UnsavedRelationList($parentClass, $componentName, $componentClass);
         }
         return $this->unsavedRelations[$componentName];
     }
     $extraFields = $this->manyManyExtraFieldsForComponent($componentName) ?: array();
     /** @var ManyManyList $result */
     $result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $extraFields);
     // Store component data in query meta-data
     $result = $result->alterDataQuery(function ($query) use($extraFields) {
         $query->setQueryParam('Component.ExtraFields', $extraFields);
     });
     if ($this->model) {
         $result->setDataModel($this->model);
     }
     $this->extend('updateManyManyComponents', $result);
     // If this is called on a singleton, then we return an 'orphaned relation' that can have the
     // foreignID set elsewhere.
     return $result->setDataQueryParam($this->getInheritableQueryParams())->forForeignID($this->ID);
 }
 public function testSubtractOnAManyManyList()
 {
     $allList = ManyManyList::create('DataObjectTest_Player', 'DataObjectTest_Team_Players', 'DataObjectTest_PlayerID', 'DataObjectTest_TeamID');
     $this->assertEquals(3, $allList->count(), 'Precondition; we have all 3 players connected to a team in the list');
     $teamOneID = $this->idFromFixture('DataObjectTest_Team', 'team1');
     $teamTwoID = $this->idFromFixture('DataObjectTest_Team', 'team2');
     // Captain 1 belongs to one team; team1
     $captain1 = $this->objFromFixture('DataObjectTest_Player', 'captain1');
     $this->assertEquals(array($teamOneID), $captain1->Teams()->column("ID"), 'Precondition; player2 belongs to team1');
     // Player 2 belongs to both teams: team1, team2
     $player2 = $this->objFromFixture('DataObjectTest_Player', 'player2');
     $this->assertEquals(array($teamOneID, $teamTwoID), $player2->Teams()->sort('Title')->column('ID'), 'Precondition; player2 belongs to team1 and team2');
     // We want to find the teams for player2 where the captain does not belong to
     $teamsWithoutTheCaptain = $player2->Teams()->subtract($captain1->Teams());
     // Assertions
     $this->assertEquals(1, $teamsWithoutTheCaptain->count(), 'The ManyManyList should onlu contain one team');
     $this->assertEquals($teamTwoID, $teamsWithoutTheCaptain->first()->ID, 'The ManyManyList contains the wrong team');
 }