实体列表未转换为 json 而不是我得到一个空对象数组
List of entites not converting to json instead I get an array of empty objects
我有以下代码:
public function adminListAction(Request $request)
{
if (!$this->isGranted('ROLE_ADMIN')) {
return new JsonResponse("Not granted");
}
$page = $request->query->get('page', 1);
$criteria = new DocumentaryCriteria();
$criteria->setStatus(DocumentaryStatus::PUBLISH);
$criteria->setSort([
DocumentaryOrderBy::CREATED_AT => Order::DESC
]);
$qb = $this->documentaryService->getDocumentariesByCriteriaQueryBuilder($criteria);
$adapter = new DoctrineORMAdapter($qb, false);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(12);
$pagerfanta->setCurrentPage($page);
$items = (array) $pagerfanta->getCurrentPageResults();
$data = [
'items' => $items,
'count_results' => $pagerfanta->getNbResults(),
'current_page' => $pagerfanta->getCurrentPage(),
'number_of_pages' => $pagerfanta->getNbPages(),
'next' => ($pagerfanta->hasNextPage()) ? $pagerfanta->getNextPage() : null,
'prev' => ($pagerfanta->hasPreviousPage()) ? $pagerfanta->getPreviousPage() : null,
'paginate' => $pagerfanta->haveToPaginate(),
];
return new JsonResponse($data);
}
下面是returns,注意空对象数组
{
"items": [
{},
{},
{},
{},
{},
{},
{},
{},
{}
],
"count_results": 9,
"current_page": 1,
"number_of_pages": 1,
"next": null,
"prev": null,
"paginate": false
}
通过这样做我知道它们的属性不为空:
foreach ($items as $item) {
echo $item->getTitle();
}
// returns 'Documentary 1'
问题很可能是您的 $item
对象无法 json 序列化。
尝试在 class(https://www.php.net/manual/en/class.jsonserializable.php) 中实现 JsonSerializable
接口,并向 item
class 添加一个方法,如下所示:
public function jsonSerialize() {
return [
'title' => $this->getTitle(),
'foo' => $this->bar(),
];
}
我有以下代码:
public function adminListAction(Request $request)
{
if (!$this->isGranted('ROLE_ADMIN')) {
return new JsonResponse("Not granted");
}
$page = $request->query->get('page', 1);
$criteria = new DocumentaryCriteria();
$criteria->setStatus(DocumentaryStatus::PUBLISH);
$criteria->setSort([
DocumentaryOrderBy::CREATED_AT => Order::DESC
]);
$qb = $this->documentaryService->getDocumentariesByCriteriaQueryBuilder($criteria);
$adapter = new DoctrineORMAdapter($qb, false);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(12);
$pagerfanta->setCurrentPage($page);
$items = (array) $pagerfanta->getCurrentPageResults();
$data = [
'items' => $items,
'count_results' => $pagerfanta->getNbResults(),
'current_page' => $pagerfanta->getCurrentPage(),
'number_of_pages' => $pagerfanta->getNbPages(),
'next' => ($pagerfanta->hasNextPage()) ? $pagerfanta->getNextPage() : null,
'prev' => ($pagerfanta->hasPreviousPage()) ? $pagerfanta->getPreviousPage() : null,
'paginate' => $pagerfanta->haveToPaginate(),
];
return new JsonResponse($data);
}
下面是returns,注意空对象数组
{ "items": [ {}, {}, {}, {}, {}, {}, {}, {}, {} ], "count_results": 9, "current_page": 1, "number_of_pages": 1, "next": null, "prev": null, "paginate": false }
通过这样做我知道它们的属性不为空:
foreach ($items as $item) {
echo $item->getTitle();
}
// returns 'Documentary 1'
问题很可能是您的 $item
对象无法 json 序列化。
尝试在 class(https://www.php.net/manual/en/class.jsonserializable.php) 中实现 JsonSerializable
接口,并向 item
class 添加一个方法,如下所示:
public function jsonSerialize() {
return [
'title' => $this->getTitle(),
'foo' => $this->bar(),
];
}