Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pager Fanta && Serializer Group not working #1880

Open
lmasforne opened this issue Mar 15, 2018 · 0 comments
Open

Pager Fanta && Serializer Group not working #1880

lmasforne opened this issue Mar 15, 2018 · 0 comments

Comments

@lmasforne
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant