【发布时间】:2019-02-05 16:16:51
【问题描述】:
我在 symfony 4.2 中使用序列化来解码一个 json 并将一个实体保存到数据库中,问题是日期时间,当我反序列化 symfony 时给我一个错误,它需要一个日期时间对象并且给出了字符串。
我检查了我的控件中的所有“使用”语句,以及所有带有属性信息的配置,让 symfony 识别 json 的所有字段并弄清楚它是什么。
链接到 git 项目:
https://github.com/erethilclaw/lair_of_claw
/**
* @Route("admin/newPost", name="newPost", methods={"POST"})
*/
public function newPost( Request $request){
/** @var Serializer $serializer */
$serializer = $this->get('serializer');
$post = $serializer->deserialize($request->getContent(),
Post::class, 'json');
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
return $this->json($post);
}
我正在传递的 Json
{
"title": "A third blog post!",
"published_at": "2018-07-01 12:00:00",
"content": "Hello there!",
"author": "Piotr Jura",
"slug": "a-third-blog-post"
}
使用反序列化方法应将“published_at”转换为日期时间以保存到数据库中,但给我错误:“在属性路径“published_at”中给出的“预期参数类型为“DateTiemInterface”、“字符串”。
感谢您的宝贵时间
【问题讨论】:
标签: json symfony4 json-deserialization serialization