Permalink
Browse files

API CHANGE: add a remote relation class getter to DataObject

  • Loading branch information...
1 parent 47e3052 commit 6469d83569f24cfe6d5d39061996f68c8f7c0ea9 @mateusz mateusz committed Apr 20, 2012
Showing with 41 additions and 1 deletion.
  1. +32 −1 model/DataObject.php
  2. +9 −0 tests/model/DataObjectTest.php
View
33 model/DataObject.php
@@ -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'.
View
9 tests/model/DataObjectTest.php
@@ -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 */

0 comments on commit 6469d83

Please sign in to comment.