【发布时间】:2019-03-04 02:21:09
【问题描述】:
最近的 Silverstripe 版本升级到 4.3.1 似乎破坏了将数据对象作为 JSON 加载到文本字段中的某些功能。
对象如下所示:
class Foo extends DataObject
private static $db = [
'Name' => 'Varchar',
'Description' => 'Text',
'Models' => 'Text',
];
然后有一个函数可以使用从表单请求生成的 JSON 加载对象:
$data = json_decode($request->getBody(), true);
$foo = new Foo();
$foo->update($data);
以下是 JSON $data 的示例:
"Name":"Test",
"Description":"Wangle fangle blurble wurgle.",
"Models":{
"fish":{"trout":10,"salmon":15,"sturgeon":20},
"vegetable":{"carrot":1,"cabbage":2,"leek":3},
"sauce":{"chipotle":6,"tomato":4,"soy":2}
}
直到最近,“模型”结构才会作为文本保存到“模型”字段中:
"fish":{"trout":10,"salmon":15,"sturgeon":20},
"vegetable":{"carrot":1,"cabbage":2,"leek":3},
"sauce":{"chipotle":6,"tomato":4,"soy":2}
但现在我们得到以下错误:
DataObject::setField: Models only accepts scalars at /var/www/example/vendor/silverstripe/framework/src/ORM/DataObject.php:2648
DataObject.php 中的第 2640 行说:
If this is a proper database field, we shouldn't be getting non-DBField objects
最近是否有阻止尝试将 JSON 对象加载到字段中的安全修复程序?
谁能帮忙将模型 JSON 保存到文本字段中?
【问题讨论】:
-
可能这个问题是相关的:github.com/silverstripe/silverstripe-framework/issues/8814 我看到了一些可能的解决方法:1)转义 json 2)创建自己的 DBField 类来接受非标量值。您当前将一个数组传递给它或 3)根据 php.net/manual/en/function.json-decode.php 调整您的 json_decode 的深度。也许
json_decode($request->getBody(), true, 1)能解决问题?另见:php.net/is_scalar
标签: php silverstripe silverstripe-4