Sprig_Core::relate PHP Method

relate() public method

Adds a relationship to the model
public relate ( $name, $value )
    public function relate($name, $value)
    {
        if (!isset($this->_fields[$name]) or !$this->_fields[$name] instanceof Sprig_Field_HasMany) {
            throw new Sprig_Exception('Unknown relationship: :name', array(':name' => $name));
        }
        $values = array();
        if (is_object($value)) {
            $values[$value->{$value->pk()}] = $value->{$value->pk()};
        } elseif (is_numeric($value)) {
            $values[$value] = $value;
        } elseif (is_array($value)) {
            $values = $value;
        } else {
            throw new Sprig_Exception('Invalid data type: :value', array(':value' => $value));
        }
        $this->{$name} = arr::get($this->_original, $name, array()) + $values;
        return $this;
    }