public function coreSetRelated($name, $value)
{
$rel = $this->_table->getRelation($name);
if ($value === null) {
$value = self::$_null;
}
// one-to-many or one-to-one relation
if ($rel instanceof Doctrine_Relation_ForeignKey || $rel instanceof Doctrine_Relation_LocalKey) {
if (!$rel->isOneToOne()) {
// one-to-many relation found
if (!$value instanceof Doctrine_Collection) {
throw new Doctrine_Record_Exception("Couldn't call Doctrine_Core::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.");
}
if (isset($this->_references[$name])) {
$this->_references[$name]->setData($value->getData());
return $this;
}
} else {
$localFieldName = $this->_table->getFieldName($rel->getLocal());
if ($value !== self::$_null) {
$relatedTable = $rel->getTable();
$foreignFieldName = $relatedTable->getFieldName($rel->getForeign());
}
// one-to-one relation found
if (!$value instanceof Doctrine_Record && !$value instanceof Doctrine_Null) {
throw new Doctrine_Record_Exception("Couldn't call Doctrine_Core::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) {
if ($value !== self::$_null && !empty($foreignFieldName) && $foreignFieldName != $value->getTable()->getIdentifier()) {
$this->set($localFieldName, $value->rawGet($foreignFieldName), false);
} else {
// FIX: Ticket #1280 fits in this situation
$this->set($localFieldName, $value, false);
}
} elseif ($value !== self::$_null) {
// We should only be able to reach $foreignFieldName if we have a Doctrine_Record on hands
$value->set($foreignFieldName, $this, false);
}
}
} else {
if ($rel instanceof Doctrine_Relation_Association) {
// join table relation found
if (!$value instanceof Doctrine_Collection) {
throw new Doctrine_Record_Exception("Couldn't call Doctrine_Core::set(), second argument should be an instance of Doctrine_Collection when setting many-to-many references.");
}
}
}
$this->_references[$name] = $value;
}