Php 页码页码范塔符号4
我用Symfony4和 我使用PagerFanta包。我为分页创建了一个AbstractRepository抽象类:Php 页码页码范塔符号4,php,dql,symfony4,Php,Dql,Symfony4,我用Symfony4和 我使用PagerFanta包。我为分页创建了一个AbstractRepository抽象类: <?php namespace App\Repository; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; use Pagerfanta\Adapter\DoctrineORMAdapter; use Pagerfanta\Pagerfanta; class AbstractRep
<?php
namespace App\Repository;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
class AbstractRepository extends EntityRepository
{
protected function paginate(QueryBuilder $qb, $limit = 20, $offset = 0)
{
if (0 == $limit || 0 == $offset) {
throw new \LogicException('$limit & $offstet 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;
}
}
My Entity Article:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\ExclusionPolicy;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class Article
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
* @ORM\Table(name="article")
*
* @ExclusionPolicy("all")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\Column(type="integer")
* @ORM\GeneratedValue()
*
* @Expose()
*/
private $id;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank(groups={"Create"})
* @Expose()
*/
private $title;
/**
* @ORM\Column(type="string")
* @Assert\NotBlank(groups={"Create"})
* @Expose()
*/
private $content;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* @param mixed $title
*/
public function setTitle($title): void
{
$this->title = $title;
}
/**
* @return mixed
*/
public function getContent()
{
return $this->content;
}
/**
* @param mixed $content
*/
public function setContent($content): void
{
$this->content = $content;
}
}
我在AbstractRepository中看到了我的问题
$currentPage = ceil($offset + 1) / $limit;
per:
还有我的朋友
* @Rest\QueryParam(
* name="offset",
* requirements="\d+",
* default="0",
* description="The pagination offset"
* )
* @Rest\View()
*/
谢谢
<?php
namespace App\Representation;
use Pagerfanta\Pagerfanta;
use JMS\Serializer\Annotation\Type;
class Articles
{
/**
* @Type("ArrayCollection<App\Entity\Article>")
*/
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;
}
}
$currentPage = ceil($offset + 1) / $limit;
$currentPage = ceil(($offset + 1) / $limit);
* @Rest\QueryParam(
* name="offset",
* requirements="\d+",
* default="0",
* description="The pagination offset"
* )
* @Rest\View()
*/
* @Rest\QueryParam(
* name="offset",
* requirements="\d+",
* default="1",
* description="The pagination offset"
* )
* @Rest\View()
*/