JMS 组序列化和 PagerFantaBundle



我正在使用Symfony和FOSRESTBundle开发RESTFull API。我将 JMS 序列化程序与组一起使用,以根据上下文显示不同的字段。

为了列出我的资源,我想实现PagerFantaBundle。除了PagerFanta不考虑序列化组之外,所有工作正常。

在我的代码下面:

分页功能:

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);
}

控制器:

/**
* @RestGet(
*     path = "/apartments",
*     name = "apartments_get_all"
* )
* @RestQueryParam(
*     name="order",
*     requirements="asc|desc",
*     nullable=true,
*     description="Sort order (asc or desc)"
* )
* @RestQueryParam(
*     name="limit",
*     requirements="d+",
*     default="20",
*     description="Max number of apartments per page"
* )
* @RestQueryParam(
*     name="offset",
*     requirements="d+",
*     default="0",
*     description="Pagination offset"
* )
* @RestView(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<HGBCoreBundleEntityApartment>")
*/
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;
}
}

公寓实体 :

/**
* Apartment
*
* @ORMTable(name="apartment")
* @ORMEntity(repositoryClass="HGBCoreBundleRepositoryApartmentRepository")
* @JMSExclusionPolicy("all")
*/
class Apartment
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @JMSGroups({"listApartment"})
* @JMSExpose()
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="title", type="string", length=200)
* @JMSGroups({"listApartment"})
* @JMSExpose()
* @AssertNotBlank()
*/
private $title;
...

那么,如何将组序列化与PagerFanta一起使用,或者我可以将什么分页系统用于组序列化?

谢谢

我已经找到了解决方案。只需要像这样为公寓表示形式配置序列化组:

<?php

namespace HGBCoreBundleRepresentation;

use JMSSerializerAnnotation as Serializer;
use PagerfantaPagerfanta;
/**
* Class Apartments
* @package HGBCoreBundleRepresentation
* @SerializerExclusionPolicy("all")
*/
class Apartments
{
/**
* @SerializerType("array<HGBCoreBundleEntityApartment>")
* @SerializerGroups("listApartment")
* @SerializerExpose()
*/
public $data;
/**
* @SerializerGroups("listApartment")
* @SerializerExpose()
*/
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;
}
}

相关内容

  • 没有找到相关文章

最新更新