public function getManyManyComponents($componentName, $filter = "", $sort = "", $join = "", $limit = "") { list($parentClass, $componentClass, $parentField, $componentField, $table) = $this->many_many($componentName); // 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] = UnsavedRelationList::create($parentClass, $componentName, $componentClass); } return $this->unsavedRelations[$componentName]; } $result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $this->many_many_extraFields($componentName)); if ($this->model) { $result->setDataModel($this->model); } // If this is called on a singleton, then we return an 'orphaned relation' that can have the // foreignID set elsewhere. $result = $result->forForeignID($this->ID); return $result->where($filter)->sort($sort)->limit($limit); }
/** * Returns a many-to-many component, as a ManyManyList. * @param string $componentName Name of the many-many component * @return ManyManyList The set of components * * @todo Implement query-params */ public function getManyManyComponents($componentName, $filter = null, $sort = null, $join = null, $limit = null) { list($parentClass, $componentClass, $parentField, $componentField, $table) = $this->manyManyComponent($componentName); if ($filter !== null || $sort !== null || $join !== null || $limit !== null) { Deprecation::notice('4.0', 'The $filter, $sort, $join and $limit parameters for DataObject::getManyManyComponents() have been deprecated. Please manipluate the returned list directly.', Deprecation::SCOPE_GLOBAL); } // 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]; } $result = ManyManyList::create($componentClass, $table, $componentField, $parentField, $this->manyManyExtraFieldsForComponent($componentName)); 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->forForeignID($this->ID)->where($filter)->sort($sort)->limit($limit); }
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'); }
/** * 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); 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); }