Horde_Rdo_Mapper::addRelation PHP Method

addRelation() public method

- For one-to-one relations, simply updates the relation field. - For one-to-many relations, updates the related object's relation field. - For many-to-many relations, adds an entry in the "through" table. - Performs a no-op if the peer is already related.
public addRelation ( string $relationship, Horde_Rdo_Base $ours, Horde_Rdo_Base $theirs )
$relationship string The relationship key in the mapper.
$ours Horde_Rdo_Base The object from this mapper to add the relation.
$theirs Horde_Rdo_Base The other object from any mapper to add the relation.
    public function addRelation($relationship, Horde_Rdo_Base $ours, Horde_Rdo_Base $theirs)
    {
        if ($ours->hasRelation($relationship, $theirs)) {
            return;
        }
        $ourKey = $this->primaryKey;
        $theirKey = $theirs->mapper->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']} = $theirs->{$theirKey};
                $ours->save();
                break;
            case Horde_Rdo::ONE_TO_MANY:
                $theirs->{$rel['foreignKey']} = $ours->{$ourKey};
                $theirs->save();
                break;
            case Horde_Rdo::MANY_TO_MANY:
                $sql = sprintf('INSERT INTO %s (%s, %s) VALUES (?, ?)', $this->adapter->quoteTableName($rel['through']), $this->adapter->quoteColumnName($ourKey), $this->adapter->quoteColumnName($theirKey));
                try {
                    $this->adapter->insert($sql, array($ours->{$ourKey}, $theirs->{$theirKey}));
                } catch (Horde_Db_Exception $e) {
                    throw new Horde_Rdo_Exception($e);
                }
                break;
        }
    }