Skip to content

Commit

Permalink
API CHANGE: add a remote relation class getter to DataObject
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz committed Apr 23, 2012
1 parent 47e3052 commit 6469d83
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
33 changes: 32 additions & 1 deletion model/DataObject.php
Expand Up @@ -1342,7 +1342,38 @@ public function getComponentsQuery($componentName, $filter = "", $sort = "", $jo

return singleton($componentClass)->extendedSQL($combinedFilter, $sort, $limit, $join);
}


/**
* Find the foreign class of a relation on this DataObject, regardless of the relation type.
*
* @param $relationName Relation name.
* @return string Class name, or null if not found.
*/
public function getRelationClass($relationName) {
// Go through all relationship configuration fields.
$candidates = array_merge(
($relations = Config::inst()->get($this->class, 'has_one')) ? $relations : array(),
($relations = Config::inst()->get($this->class, 'has_many')) ? $relations : array(),
($relations = Config::inst()->get($this->class, 'many_many')) ? $relations : array(),
($relations = Config::inst()->get($this->class, 'belongs_many_many')) ? $relations : array(),
($relations = Config::inst()->get($this->class, 'belongs_to')) ? $relations : array()
);

if (isset($candidates[$relationName])) {
$remoteClass = $candidates[$relationName];

// If dot notation is present, extract just the first part that contains the class.
if(($fieldPos = strpos($remoteClass, '.'))!==false) {
return substr($remoteClass, 0, $fieldPos);
}

// Otherwise just return the class
return $remoteClass;
}

return null;
}

/**
* Tries to find the database key on another object that is used to store a relationship to this class. If no join
* field can be found it defaults to 'ParentID'.
Expand Down
9 changes: 9 additions & 0 deletions tests/model/DataObjectTest.php
Expand Up @@ -183,6 +183,15 @@ function testGetSubclassFields() {
$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $captain1->FavouriteTeamID);
}

function testGetRelationClass() {
$obj = new DataObjectTest_Player();
$this->assertEquals(singleton('DataObjectTest_Player')->getRelationClass('FavouriteTeam'), 'DataObjectTest_Team', 'has_one is properly inspected');
$this->assertEquals(singleton('DataObjectTest_Company')->getRelationClass('CurrentStaff'), 'DataObjectTest_Staff', 'has_many is properly inspected');
$this->assertEquals(singleton('DataObjectTest_Team')->getRelationClass('Players'), 'DataObjectTest_Player', 'many_many is properly inspected');
$this->assertEquals(singleton('DataObjectTest_Player')->getRelationClass('Teams'), 'DataObjectTest_Team', 'belongs_many_many is properly inspected');
$this->assertEquals(singleton('DataObjectTest_CEO')->getRelationClass('Company'), 'DataObjectTest_Company', 'belongs_to is properly inspected');
}

function testGetHasOneRelations() {
$captain1 = $this->objFromFixture("DataObjectTest_Player", "captain1");
/* There will be a field called (relname)ID that contains the ID of the object linked to via the has_one relation */
Expand Down

0 comments on commit 6469d83

Please sign in to comment.