Skip to content

Commit

Permalink
Merge branch 'wakes-changes'
Browse files Browse the repository at this point in the history
* wakes-changes:
  Relationships now use the actual ClassName of the related model not that specified by the relationship for when the related model instance is derived from that specified in the relationship.
  • Loading branch information
camspiers committed Aug 14, 2014
2 parents a773492 + 60f1e23 commit 0fc7b5f
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions src/Camspiers/SilverStripe/FixtureGenerator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Generator
* This mode excludes relations specified
*/
const RELATION_MODE_EXCLUDE = 1;
/**
* This mode excludes generation of related objects (however relationships may still be generated)
*/
const RELATED_OBJECT_EXCLUDE = 2;
/**
* @var DumperInterface
*/
Expand Down Expand Up @@ -81,12 +85,17 @@ private function generateFromDataObject(DataObject $dataObject, array &$map = ar
if ($this->isAllowedRelation("$className.$relName")) {
// Get the dataobject from the relation
$hasOne = $dataObject->$relName();

$relClassName = $hasOne->ClassName;

// Only process it if it exists
if ($hasOne->exists() && !$this->hasDataObject($hasOne, $map)) {
// Recursively generate a map for this object
$this->generateFromDataObject($hasOne, $map);
if (($this->mode & self::RELATED_OBJECT_EXCLUDE) === 0) {
// Recursively generate a map for this object
$this->generateFromDataObject($hasOne, $map);
}
// Add the relation to the current dataobjects map
$map[$className][$id][$relName] = "=>$relClass." . $hasOne->ID;
$map[$className][$id][$relName] = "=>$relClassName." . $hasOne->ID;
}
}
}
Expand All @@ -101,20 +110,25 @@ private function generateFromDataObject(DataObject $dataObject, array &$map = ar
if ($items instanceof IteratorAggregate && count($items) > 0) {
// Loops of each dataobject
foreach ($items as $hasMany) {

$relClassName = $hasMany->ClassName;

// Only process it if it exists
if ($hasMany->exists() && !$this->hasDataObject($hasMany, $map)) {
// Recursively generate a map for this object
$this->generateFromDataObject($hasMany, $map);
if (($this->mode & self::RELATED_OBJECT_EXCLUDE) === 0) {
// Recursively generate a map for this object
$this->generateFromDataObject($hasMany, $map);
}
// Add the relation to the original objects map
if (!isset($map[$className][$id][$relName])) {
$map[$className][$id] = array_merge(
$map[$className][$id],
array(
$relName => "=>$relClass." . $hasMany->ID
$relName => "=>$relClassName." . $hasMany->ID
)
);
} else {
$map[$className][$id][$relName] .= ", =>$relClass." . $hasMany->ID;
$map[$className][$id][$relName] .= ", =>$relClassName." . $hasMany->ID;
}
}
}
Expand All @@ -133,19 +147,24 @@ private function generateFromDataObject(DataObject $dataObject, array &$map = ar
// Loops of each dataobject
foreach ($items as $manyMany) {
// Only process it if it exists

$relClassName = $manyMany->ClassName;

if ($manyMany->exists() && !$this->hasDataObject($manyMany, $map)) {
// Recursively generate a map for this object
$this->generateFromDataObject($manyMany, $map);
if (($this->mode & self::RELATED_OBJECT_EXCLUDE) === 0) {
// Recursively generate a map for this object
$this->generateFromDataObject($manyMany, $map);
}
// Add the relation to the original objects map
if (!isset($map[$className][$id][$relName])) {
$map[$className][$id] = array_merge(
$map[$className][$id],
array(
$relName => "=>$relClass." . $manyMany->ID
$relName => "=>$relClassName." . $manyMany->ID
)
);
} else {
$map[$className][$id][$relName] .= ", =>$relClass." . $manyMany->ID;
$map[$className][$id][$relName] .= ", =>$relClassName." . $manyMany->ID;
}
}
}
Expand Down

0 comments on commit 0fc7b5f

Please sign in to comment.