diff --git a/lib/Doctrine/Relation/Parser.php b/lib/Doctrine/Relation/Parser.php index a48c972af..2c17c6107 100644 --- a/lib/Doctrine/Relation/Parser.php +++ b/lib/Doctrine/Relation/Parser.php @@ -74,6 +74,15 @@ class Doctrine_Relation_Parser return $this->_pending[$name]; } + + public function hasRelation($name) + { + if ( ! isset($this->_pending[$name]) && ! isset($this->_relations[$name])) { + return false; + } + + return true; + } /** * binds a relation * @@ -190,17 +199,21 @@ class Doctrine_Relation_Parser if ( ! isset($this->_pending[$def['refClass']]) && ! isset($this->_relations[$def['refClass']])) { - $def['refTable']->getRelationParser()->bind($this->_table->getComponentName(), - array('type' => Doctrine_Relation::ONE, - 'local' => $def['local'], - 'foreign' => $this->_table->getIdentifier(), - 'localKey' => true, - )); - - $this->bind($def['refClass'], array('type' => Doctrine_Relation::MANY, - 'foreign' => $def['local'], - 'local' => $this->_table->getIdentifier())); + $parser = $def['refTable']->getRelationParser(); + if ( ! $parser->hasRelation($this->_table->getComponentName())) { + $parser->bind($this->_table->getComponentName(), + array('type' => Doctrine_Relation::ONE, + 'local' => $def['local'], + 'foreign' => $this->_table->getIdentifier(), + 'localKey' => true, + )); + } + if ( ! $this->hasRelation($def['refClass'])) { + $this->bind($def['refClass'], array('type' => Doctrine_Relation::MANY, + 'foreign' => $def['local'], + 'local' => $this->_table->getIdentifier())); + } } if (in_array($def['class'], $localClasses)) { $rel = new Doctrine_Relation_Nest($def); diff --git a/lib/Doctrine/Table.php b/lib/Doctrine/Table.php index 2d66ec6e5..258847b5d 100644 --- a/lib/Doctrine/Table.php +++ b/lib/Doctrine/Table.php @@ -211,8 +211,8 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable if (isset($this->columns)) { // get the declaring class of setTableDefinition method - $method = new ReflectionMethod($this->options['name'], 'setTableDefinition'); - $class = $method->getDeclaringClass(); + $method = new ReflectionMethod($this->options['name'], 'setTableDefinition'); + $class = $method->getDeclaringClass(); $this->options['declaringClass'] = $class; @@ -364,7 +364,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable if ($relation instanceof Doctrine_Relation_LocalKey) { $def = array('local' => $relation->getLocal(), - 'foreign' => $this->getIdentifier(), + 'foreign' => $relation->getForeign(), 'foreignTable' => $relation->getTable()->getTableName()); if (($key = array_search($def, $options['foreignKeys'])) === false) {