public static function save_from_map(DataObject $record, $relation, $regField = '', $write = true)
    {
        $map = $record->toMap();
        $rel = $record->getComponent($relation);
        $changed = false;
        if (!$regField) {
            $regField = $relation;
        }

        foreach ($map as $field => $val) {
            if (preg_match('/^' . preg_quote($regField) . '\[(\w+)\]$/', $field, $match) !== false) {
                if (count($match) == 2 && isset($match[1])) {
                    $rel->setCastedField($match[1], $val);
                    $changed = true;
                }
            }
        }

        if ($changed) {
            if ($write) {
                $rel->write();
            }

            return true;
        }

        return false;
    }