diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index be6115c1c..879e8bd8f 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -883,7 +883,11 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count throw new Doctrine_Record_Exception("Couldn't call Doctrine::set(), second argument should be an instance of Doctrine_Record or Doctrine_Null when setting one-to-one references."); } if ($rel instanceof Doctrine_Relation_LocalKey) { - $this->set($rel->getLocal(), $value->rawGet($rel->getForeign()), false); + $foreign = $rel->getForeign(); + if (!empty($foreign) && $foreign != $value->getTable()->getIdentifier()) + $this->set($rel->getLocal(), $value->rawGet($foreign), false); + else + $this->set($rel->getLocal(), $value, false); } else { $value->set($rel->getForeign(), $this, false); } diff --git a/lib/Doctrine/Relation/LocalKey.php b/lib/Doctrine/Relation/LocalKey.php index 33452f2a1..9a89153dd 100644 --- a/lib/Doctrine/Relation/LocalKey.php +++ b/lib/Doctrine/Relation/LocalKey.php @@ -44,7 +44,7 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation public function fetchRelatedFor(Doctrine_Record $record) { $id = $record->get($this->definition['local']); - + if (empty($id) || ! $this->definition['table']->getAttribute(Doctrine::ATTR_LOAD_REFERENCES)) { $related = $this->getTable()->create(); } else { @@ -56,11 +56,11 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation ->query($dql, array($id)) ->getFirst(); - if ( ! $related) { - $related = $this->getTable()->create(); + if ( ! $related || empty($related)) { + $related = $this->getTable()->create(); } } - + $record->set($this->definition['local'], $related, false); return $related; @@ -72,7 +72,7 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation * @param string $alias */ public function getCondition($alias = null) - { + { if ( ! $alias) { $alias = $this->getTable()->getComponentName(); }