Commit 70addc55 authored by Jonathan.Wage's avatar Jonathan.Wage

Fixes to model building so it does not generate duplicate relations.

parent 13a79c45
......@@ -212,7 +212,24 @@ class Doctrine_Import_Schema
*/
public function getRelations($properties)
{
return isset($this->_relations[$properties['className']]) ? $this->_relations[$properties['className']]:array();
$allRelations = isset($this->_relations[$properties['className']]) ? $this->_relations[$properties['className']]:array();
// This is for checking for duplicates between alias-relations and a auto-generated relations to ensure the result set of unique relations
$existingRelations = array();
$uniqueRelations = array();
foreach ($allRelations as $relation) {
if ( ! in_array($relation['key'], $existingRelations)) {
$existingRelations[] = $relation['key'];
$uniqueRelations = array_merge($uniqueRelations, array($relation['alias'] => $relation));
} else {
// check to see if this relationship is not autogenerated, if it's not, then the user must have explicitly declared it
if (!isset($relation['autogenerated']) || $relation['autogenerated'] != true) {
$uniqueRelations = array_merge($uniqueRelations, array($relation['alias'] => $relation));
}
}
}
return $uniqueRelations;
}
/**
......@@ -376,7 +393,7 @@ class Doctrine_Import_Schema
* @param string $array
* @return void
*/
protected function _buildRelationships(&$array)
protected function _buildRelationships($array)
{
foreach ($array as $name => $properties) {
if ( ! isset($properties['relations'])) {
......@@ -415,6 +432,8 @@ class Doctrine_Import_Schema
$relation['foreignType'] = $relation['foreignType'] === 'one' ? Doctrine_Relation::ONE:Doctrine_Relation::MANY;
}
$relation['key'] = $this->_buildUniqueRelationKey($relation);
$this->_relations[$className][$alias] = $relation;
}
}
......@@ -456,9 +475,21 @@ class Doctrine_Import_Schema
}
if (!isset($this->_relations[$relation['class']][$newRelation['alias']])) {
$newRelation['key'] = $this->_buildUniqueRelationKey($newRelation);
$this->_relations[$relation['class']][$newRelation['alias']] = $newRelation;
}
}
}
}
/**
* _buildUniqueRelationKey
*
* @param string $relation
* @return void
*/
protected function _buildUniqueRelationKey($relation)
{
return md5($relation['local'].$relation['foreign'].$relation['class'].(isset($relation['refClass']) ? $relation['refClass']:null));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment