Php JMS组序列化和PagerFantaBundle
我正在使用Symfony和FOSRESTBundle开发一个RESTFull API。我使用带有组的JMS序列化程序根据上下文显示不同的字段 为了列出我的资源,我需要实现PagerFantaBundle。除了PagerFanta不考虑序列化组之外,所有这些都可以正常工作 下面是我的代码: 分页功能:Php JMS组序列化和PagerFantaBundle,php,symfony,pagerfanta,Php,Symfony,Pagerfanta,我正在使用Symfony和FOSRESTBundle开发一个RESTFull API。我使用带有组的JMS序列化程序根据上下文显示不同的字段 为了列出我的资源,我需要实现PagerFantaBundle。除了PagerFanta不考虑序列化组之外,所有这些都可以正常工作 下面是我的代码: 分页功能: protected function paginate(QueryBuilder $qb, $limit = 20, $offset = 0) { if($limit == 0 || $o
protected function paginate(QueryBuilder $qb, $limit = 20, $offset = 0) {
if($limit == 0 || $offset < 0) {
throw new \LogicException("$limit & $offset must be greater than 0");
}
$pager = new Pagerfanta(new DoctrineORMAdapter($qb));
$currentPage = ceil($offset + 1 / $limit);
$pager->setCurrentPage($currentPage);
$pager->setMaxPerPage((int) $limit);
return $pager;
}
public function listApartments(Location $location, $order = 'asc', $limit = 20, $offset = 0) {
$qb = $this->createQueryBuilder('a')
->select('a')
->orderBy('a.title', $order);
return $this->paginate($qb, $limit, $offset);
}
控制器:
/**
* @Rest\Get(
* path = "/apartments",
* name = "apartments_get_all"
* )
* @Rest\QueryParam(
* name="order",
* requirements="asc|desc",
* nullable=true,
* description="Sort order (asc or desc)"
* )
* @Rest\QueryParam(
* name="limit",
* requirements="\d+",
* default="20",
* description="Max number of apartments per page"
* )
* @Rest\QueryParam(
* name="offset",
* requirements="\d+",
* default="0",
* description="Pagination offset"
* )
* @Rest\View(statusCode=200, serializerGroups={"listApartment"})
*/
public function getApartmentsAction(ParamFetcherInterface $paramFetcher)
{
$pager = $this->getDoctrine()->getRepository("HGBCoreBundle:Apartment")->listApartments(
new Location(),
$paramFetcher->get('order'),
$paramFetcher->get('limit'),
$paramFetcher->get('offset')
);
return new Apartments($pager);
}
公寓代表:
class Apartments
{
/**
* @Type("array<HGB\CoreBundle\Entity\Apartment>")
*/
public $data;
public $meta;
public function __construct(Pagerfanta $data)
{
$this->data = $data->getCurrentPageResults();
$this->addMeta('limit', $data->getMaxPerPage());
$this->addMeta('current_items', count($data->getCurrentPageResults()));
$this->addMeta('total_items', $data->getNbResults());
$this->addMeta('offset', $data->getCurrentPageOffsetStart());
}
public function addMeta($name, $value)
{
if (isset($this->meta[$name])) {
throw new \LogicException(sprintf('This meta already exists. You are trying to override this meta, use the setMeta method instead for the %s meta.', $name));
}
$this->setMeta($name, $value);
}
public function setMeta($name, $value)
{
$this->meta[$name] = $value;
}
}
那么,如何在PagerFanta中使用组序列化,或者在组序列化中使用什么分页系统
谢谢,我找到了解决办法。只需按如下方式配置公寓表示的序列化组:
<?php
namespace HGB\CoreBundle\Representation;
use JMS\Serializer\Annotation as Serializer;
use Pagerfanta\Pagerfanta;
/**
* Class Apartments
* @package HGB\CoreBundle\Representation
* @Serializer\ExclusionPolicy("all")
*/
class Apartments
{
/**
* @Serializer\Type("array<HGB\CoreBundle\Entity\Apartment>")
* @Serializer\Groups("listApartment")
* @Serializer\Expose()
*/
public $data;
/**
* @Serializer\Groups("listApartment")
* @Serializer\Expose()
*/
public $meta;
public function __construct(Pagerfanta $data)
{
$this->data = $data;
$this->addMeta('limit', $data->getMaxPerPage());
$this->addMeta('current_items', count($data->getCurrentPageResults()));
$this->addMeta('total_items', $data->getNbResults());
$this->addMeta('offset', $data->getCurrentPageOffsetStart());
}
public function addMeta($name, $value)
{
if (isset($this->meta[$name])) {
throw new \LogicException(sprintf('This meta already exists. You are trying to override this meta, use the setMeta method instead for the %s meta.', $name));
}
$this->setMeta($name, $value);
}
public function setMeta($name, $value)
{
$this->meta[$name] = $value;
}
}
<?php
namespace HGB\CoreBundle\Representation;
use JMS\Serializer\Annotation as Serializer;
use Pagerfanta\Pagerfanta;
/**
* Class Apartments
* @package HGB\CoreBundle\Representation
* @Serializer\ExclusionPolicy("all")
*/
class Apartments
{
/**
* @Serializer\Type("array<HGB\CoreBundle\Entity\Apartment>")
* @Serializer\Groups("listApartment")
* @Serializer\Expose()
*/
public $data;
/**
* @Serializer\Groups("listApartment")
* @Serializer\Expose()
*/
public $meta;
public function __construct(Pagerfanta $data)
{
$this->data = $data;
$this->addMeta('limit', $data->getMaxPerPage());
$this->addMeta('current_items', count($data->getCurrentPageResults()));
$this->addMeta('total_items', $data->getNbResults());
$this->addMeta('offset', $data->getCurrentPageOffsetStart());
}
public function addMeta($name, $value)
{
if (isset($this->meta[$name])) {
throw new \LogicException(sprintf('This meta already exists. You are trying to override this meta, use the setMeta method instead for the %s meta.', $name));
}
$this->setMeta($name, $value);
}
public function setMeta($name, $value)
{
$this->meta[$name] = $value;
}
}