function ToDBString($value)
    {
        if ($value === null)
            return $value;
        
        if ($this->IsRelatedObject($value))
            return $value->KeyValue();
        
        else if ($this->IsDatabaseObject($value))
            return $this->type->ToDBString($value);
        
        else if (!is_object($value) && !is_array($value))
            return (string)$value;
        
        throw new \InvalidArgumentException('Invalid argument for fieldmapper. Object could not be converted to database string.');
    }