在Symfony2中使用json数据水合实体(Hydrate entities with json data in Symfony2)

编程入门 行业动态 更新时间:2024-10-26 16:27:33
在Symfony2中使用json数据水合实体(Hydrate entities with json data in Symfony2)

有没有办法用Symfony2中传入请求的json数据来保存实体? 我以为有类似的东西

Form::bindRequest

但是我找不到任何东西......将这个功能用于像knockout js这样的库会更好。

Is there any way to hydrate entities with json data from an incoming request in Symfony2? I thought there was something similar to

Form::bindRequest

But I can't find anything... It would be nice to have this feature for using with libraries like knockout js.

最满意答案

如果您需要的是从JSON(或XML)格式水合对象,您可以查看Serializer组件 。

这是为了解决这种常见问题。

您甚至可以尝试使用此组件更容易的JMSSerializerBundle 。

关于验证,因为它是另一个组件(Validator),您可以使用它的形式:

$validator = $this->get('validator'); $violdations = $validator->validate($myModel); if (0 === $violations->count()) { // ok ! } else { // there are some constraint violations... }

If what you need is to hydrate objects from a JSON (or XML) format, you can take a look at the Serializer component.

It was made to solve this kind of common problem.

You can even try the JMSSerializerBundle that makes the use of this component easier.

About validation, as it's another component (Validator), you can use it appart of the forms:

$validator = $this->get('validator'); $violdations = $validator->validate($myModel); if (0 === $violations->count()) { // ok ! } else { // there are some constraint violations... }

更多推荐

本文发布于:2023-08-01 15:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:水合   实体   数据   json   entities

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!