问题尝试将实体编码为json [英] Problems try encode entity to json

查看:161
本文介绍了问题尝试将实体编码为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将Doctrine实体编码为JSON字符串,以作为Ajax响应发送. 因此,我检查了文档:序列化程序组件

我尝试使用以下代码:

$em = $this->getDoctrine()->getManager();
// Get the entities repository
$sesiones_registradas = $em->getRepository('AuditBundle:AuditSession')->findAll();

// Instance the object
$serializer = new Serializer(array(new JsonEncoder()),array(new GetSetMethodNormalizer()));

// Convert only an item
foreach($sesiones_registradas as $sesion){
    echo $serializer->normalize($sesion,'json');
    break;
}
// Stop script
die();

最后一个代码,失败:

无法规范类型的对象 AppsManantiales \ AuditBundle \ Entity \ AuditSession,不支持 找到规范化器.

如果将$serializer->normalize($sesion,'json')更改为$serializer->serialize($sesion, 'json');,则错误消息为:

不支持json格式的序列化

有什么想法吗?

解决方案

使用JMS序列化程序包

可以在此处找到文档: http://jmsyst.com/bundles/JMSSerializerBundle

im try encode an Doctrine entity as JSON string, to send as Ajax response. So, i check the doc: The Serializer Component

I try with this code:

$em = $this->getDoctrine()->getManager();
// Get the entities repository
$sesiones_registradas = $em->getRepository('AuditBundle:AuditSession')->findAll();

// Instance the object
$serializer = new Serializer(array(new JsonEncoder()),array(new GetSetMethodNormalizer()));

// Convert only an item
foreach($sesiones_registradas as $sesion){
    echo $serializer->normalize($sesion,'json');
    break;
}
// Stop script
die();

Last code, fails saying:

Could not normalize object of type AppsManantiales\AuditBundle\Entity\AuditSession, no supporting normalizer found.

And if change $serializer->normalize($sesion,'json') by $serializer->serialize($sesion, 'json'); The error message is:

Serialization for the format json is not supported

Any ideas ?.

解决方案

Use the JMS Serializer Bundle

The docs can be found here: http://jmsyst.com/bundles/JMSSerializerBundle

这篇关于问题尝试将实体编码为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆