gpt4 book ai didi

json - 问题尝试将实体编码为 json

转载 作者:行者123 更新时间:2023-12-04 04:34:43 27 4
gpt4 key购买 nike

我尝试将 Doctrine 实体编码为 JSON 字符串,以作为 Ajax 响应发送。所以,我查看了文档:The Serializer Component

我尝试使用这段代码:

$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();

最后的代码,失败说:

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

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

Serialization for the format json is not supported

有什么想法吗?

最佳答案

您的问题来自您反转 normalizersencoders 的事实。

线路:
$serializer = new Serializer(array(new JsonEncoder()),array(new GetSetMethodNormalizer()));
必须是:
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array(new JsonEncoder()));

关于json - 问题尝试将实体编码为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19953802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com