1

我正在开发一个扩展,用 Bolt cms 中的单选框/复选框替换选择字段类型。我的问题是如何使用螺栓内部来存储选定的值。

当我按下保存时,Ajax POST 数据:

day[]: Monday
day[]: Friday

所以这与选择字段相同。

我遵循本教程:https ://docs.bolt.cm/extensions/customfields 并使用

public function getStorageType(){
    return 'text';
}

对此字段的 ajax-save 请求的响应是 Array,这就是进入数据库的内容。有趣的是,选择字段未出现在此响应中。我找不到选择字段数据存储在应用程序代码中的位置。

如何将其正确存储到数据库中?使用 getStorageType 'json-array' 会导致 Bolt 无法解决的错误数据库方案。

您可以在这里查看代码: https ://github.com/osfriese/bolt-boxselect/tree/develope

请帮忙。谢谢托比

4

1 回答 1

0

I found the solution - more or less.

Sadly it is hardcoded in src/Content.php. If you want a custom fieltype wich stores array values you have to change the select case in function getValues to:

                    default:
                        if (is_array($this->values[$field])) {
                            $newvalue[$field] = json_encode($this->values[$field]);
                        }else{
                            $newvalue[$field] = $this->values[$field];
                        }
                        break;

And in setValues there is a $serializedFieldTypes = array(...) where you have to manual add your custom field type.

Sadly this is not practical for extentions. But I will update my github with my changed Content.php for people who want to have a look.

When I was searching for an solution I reviewed a lot of source code of bolt master branch at github. For version 2.3 the storage layer is completely rebuild and as it seems there will be no need for any changes than. So hopefully with version >=2.3 the extention will work out of the box.

Nevertheless getStorageType() just just affect database field type and bolt just accepts 'text' here.

Hope I can help some people by answering my own question.

Thanks Tobi

于 2015-11-30T12:12:55.367 回答