“序列化数据中不支持资源."使用FOSRestBundle和Paginator时 [英] "Resources are not supported in serialized data." when using FOSRestBundle and Paginator

查看:70
本文介绍了“序列化数据中不支持资源."使用FOSRestBundle和Paginator时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FOSRestBundle和JMSSerializerBundle为我的网站设置REST服务.

I am setting up a REST service for my website with the FOSRestBundle and JMSSerializerBundle.

我在实体存储库上创建了一个自定义方法,该方法返回Paginator对象.当我在普通网站上使用该方法时效果很好,但是当我想将该方法与REST路由一起使用时,会引发此错误(XML或JSON输出会引发相同的错误):

I made a custom method on a entity repository which returns a Paginator object. The method works great when I use it on the normal website, but when I want to use the method with the REST route, this error is thrown (XML or JSON output throws the same error) :

序列化数据中不支持资源."

"Resources are not supported in serialized data."

我真的不知道在哪里搜索,因为错误对我来说不是很明显.

I really don't know where to search since the error isn't very explicit to me.

这是我的AdsRestController.php:

Here's my AdsRestController.php :

<?php

namespace MyProject\MainBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\Annotations\Get;


class AdsRestController extends Controller
{
    /**
     * @View
     * @Get("/ads/list/all/{page}", requirements={"page" = "\id+"}, defaults={"page" = 1})
     */
    public function getAdsListAllAction($page) {

        $theAds = $this->getDoctrine()->getRepository('MyProjectMainBundle:Ads')->getAds($page);

        return $theAds;
    }
}

和我的AdsRepository.php:

and my AdsRepository.php :

<?php

namespace MyProject\MainBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;

class AdsRepository extends EntityRepository
{

    public function getAds($page=1, $maxPerPage=10)
    {
            $query = $this->createQueryBuilder('a')
                ->orderBy('a.date', $order)
            ;

        $query->getQuery();

        $query
            ->setFirstResult(($page-1) * $maxPerPage)
            ->setMaxResults($maxPerPage)
        ;

        return new Paginator($query, true);
    }
}

任何帮助将不胜感激!

谢谢.

推荐答案

您可以使用iterator_to_array将分页器的迭代器转换为数组:

You can use iterator_to_array to convert iterator of your paginator into array :

return iterator_to_array($theAds->getIterator());

这篇关于“序列化数据中不支持资源."使用FOSRestBundle和Paginator时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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