FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

Pager Fanta && Serializer Group not working

Open lmasforne opened this issue 7 years ago • 0 comments

Hello,

I try to paginate with Pager Fanta like this :

    /**
     * Get User Documents
     * @param User $user
     * @param ParamFetcherInterface $paramFetcher
     * @return mixed
     */
    public function getUserDocuments(User $user, ParamFetcherInterface $paramFetcher)
    {
        $qb = $this->createQueryBuilder('d')
            ->select(['d,c'])
            ->join('d.contract' , 'c')
            ->join('c.user' , 'u')
            ->where('u.id = :userId')
            ->setParameter('userId', $user->getId());

        if ($paramFetcher->get('type')) {
            $qb->andWhere('d.type = :type')
                ->setParameter('type', $paramFetcher->get('type'));
        }

        return $this->paginate($qb, $paramFetcher->get('limit'), $paramFetcher->get('offset'));
    }
    /**
     * Paginate query
     * @param QueryBuilder $qb
     * @param int $limit
     * @param int $offset
     * @return Pagerfanta
     */
    protected function paginate(QueryBuilder $qb, $limit = 20, $offset = 0)
    {
        $limit = (int) $limit;
        if (0 === $limit) {
            throw new \LogicException('$limit must be greater than 0.');
        }

        $pager = new Pagerfanta(new DoctrineORMAdapter($qb));
        $pager->setMaxPerPage((int) $limit);
        $pager->setCurrentPage(ceil(($offset + 1) / $limit));

        return $pager;
    }
    /**
     * Get the user profile
     *
     * JWT_TOKEN  Get contracts
     *
     * @Rest\Get("/documents")
     * @Rest\View(serializerGroups={"documents"})
     * @SWG\Response(
     *     response=200,
     *     description="Returns all documents of current user",
     *     @SWG\Schema(
     *         type="array",
     *         @Model(type=User::class, groups={"documents"})
     *     )
     * )
     * @Rest\QueryParam(name="type", requirements="[A-Z]+", nullable=true, description="Optionnal document type : CONTRACT_UNSIGNED,CONTRACT_SIGNED,INVOICE")
     * @Rest\QueryParam(name="offset", requirements="\d+", nullable=true, default=0, description="Page Number")
     * @Rest\QueryParam(name="limit", requirements="\d+", nullable=true, default=25, description="NbItems displayed")
     * @SWG\Tag(name="Document")
     */
    public function getDocumentsAction(ParamFetcherInterface $paramFetcher)
    {
        /** @var User $user */
        $user = $this->getUser();

        if ($paramFetcher->get('limit') > $this->getParameter('pagination_max_limit')) {
            throw new HttpException(400, $this->get('translator')->trans('query.max.nbItems'));
        }

        return $this->getDoctrine()->getRepository('AppBundle:Document')->getUserDocuments($user , $paramFetcher);
    }

But it's return an empty object, i think there is a problem in the serialization.

If a do this :

$pagers = $this->getDoctrine()->getRepository('AppBundle:Document')->getUserDocuments($user , $paramFetcher);

foreach( $pagers as $pager) {
      echo $pager->getName();
}

I've got the name of my objects Is it normal ? Thx for you help

lmasforne avatar Mar 15 '18 15:03 lmasforne