/**
* Removes a relation to one of the relationships defined in the mapper.
*
* - For one-to-one and one-to-many relations, simply sets the relation
* field to 0.
* - For many-to-many, either deletes all relations to this object or just
* the relation to a given peer object.
* - Performs a no-op if the peer is already unrelated.
*
* This is a proxy to the mapper's removeRelation method.
*
* @param string $relationship The relationship key in the mapper.
* @param Horde_Rdo_Base $ours The object from this mapper.
* @param Horde_Rdo_Base $theirs The object to remove from the relation.
* @return integer the number of affected relations
*
* @throws Horde_Rdo_Exception
*/
public function removeRelation($relationship, Horde_Rdo_Base $ours, Horde_Rdo_Base $theirs = null)
{
if (!$ours->hasRelation($relationship, $theirs)) {
return;
}
$ourKey = $this->primaryKey;
if (isset($this->relationships[$relationship])) {
$rel = $this->relationships[$relationship];
} elseif (isset($this->lazyRelationships[$relationship])) {
$rel = $this->lazyRelationships[$relationship];
} else {
throw new Horde_Rdo_Exception('The requested relation is not defined in the mapper');
}
switch ($rel['type']) {
case Horde_Rdo::ONE_TO_ONE:
case Horde_Rdo::MANY_TO_ONE:
$ours->{$rel['foreignKey']} = null;
$ours->save();
return 1;
break;
case Horde_Rdo::ONE_TO_MANY:
$theirs->{$rel['foreignKey']} = null;
$theirs->save();
return 1;
break;
case Horde_Rdo::MANY_TO_MANY:
$sql = sprintf('DELETE FROM %s WHERE %s = ? ', $this->adapter->quoteTableName($rel['through']), $this->adapter->quoteColumnName($ourKey));
$values = array($ours->{$ourKey});
if (!empty($theirs)) {
$theirKey = $theirs->mapper->primaryKey;
$sql .= sprintf(' AND %s = ?', $this->adapter->quoteColumnName($theirKey));
$values[] = $theirs->{$theirKey};
}
try {
return $this->adapter->delete($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Rdo_Exception($e);
}
break;
}
}