问题描述
我正在使用FosrestBundle和JMSSerializerBundle为我的网站设置REST服务.
我在返回Paginator对象的实体存储库上制作了一种自定义方法.当我在普通网站上使用它时,该方法的功能很好,但是当我想将方法与其余路线一起使用时,此错误会丢弃(XML或JSON输出会引发相同的错误):
"在序列化数据中不支持资源."
我真的不知道在哪里搜索,因为错误对我来说不是很明确.
这是我的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:
<?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将Paginator的迭代器转换为数组:
return iterator_to_array($theAds->getIterator());
其他推荐答案
通过使用REST控制器中的GetAds() - > toarray()手动将结果转换为数组.
已经在这里回答,使用搜索!
其他推荐答案
查看Paginator对象上可用的 - > getIterator()方法. 请参阅 https://github.com/Doctrine/doctrine2/blob/master/lib/lib/ottrine/orm/tools/pagination/paginator.php
问题描述
I am setting up a REST service for my website with the FOSRestBundle and JMSSerializerBundle.
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.
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; } }
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); } }
Any help would be highly appreciated !
Thanks.
推荐答案
You can use iterator_to_array to convert iterator of your paginator into array :
return iterator_to_array($theAds->getIterator());
其他推荐答案
Convert result manually to an array by using getAds()->toArray() in your rest controller.
already answered here, use the search!
其他推荐答案
Check out the ->getIterator() method available on Paginator objects. See https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Tools/Pagination/Paginator.php