有没有办法用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::bindRequestBut 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... }更多推荐
发布评论