Horde_Rdo_Mapper::removeRelation PHP Method

removeRelation() public method

- 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.
public removeRelation ( string $relationship, Horde_Rdo_Base $ours, Horde_Rdo_Base $theirs = null ) : integer
$relationship string The relationship key in the mapper.
$ours Horde_Rdo_Base The object from this mapper.
$theirs Horde_Rdo_Base The object to remove from the relation.
return integer the number of affected relations
    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;
        }
    }