在Symfony2中使用json数据水化实体 [英] Hydrate entities with json data in Symfony2
问题描述
在Symfony2中,是否有任何方法可以将传入请求中的json数据与实体结合在一起? 我以为有类似的东西
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
但是我什么也找不到... 将此功能与敲除js之类的库一起使用会很好.
But I can't find anything... It would be nice to have this feature for using with libraries like knockout js.
推荐答案
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.
您甚至可以尝试使用 JMSSerializerBundle 来简化该组件的使用.
You can even try the JMSSerializerBundle that makes the use of this component easier.
关于验证,由于它是另一个组件(Validator),因此可以将其用于以下形式:
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...
}
这篇关于在Symfony2中使用json数据水化实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!