问题描述
我使用带有 JMS Serializer Bundle 的 Symfony 4.4 并在控制器中创建简单的方法:
$serializer = $this->container->get('serializer');
$response = $serializer->serialize($unitRepository->findAll(),'json',['groups' => ['normal']]);
return new JsonResponse($response);
但是返回没有数据的数组,例如:
"[[],[],[]]"
知道如何解决这个问题吗?谢谢大家的帮助:)
解决方法
您可能需要将 normal
组注释添加到您希望在响应中看到的单元实体属性中。
use JMS\Serializer\Annotation\Groups;
class Unit
{
/** @Groups({"Default","normal"}) */
protected $id;
}
或者不指定任何这样的组应该返回所有可用的属性:
$response = $serializer->serialize($unitRepository->findAll(),'json');