Knp paginator does not work with Symfony 5, it returns a query

145 Views Asked by At

I am trying to use knp paginator with Symfony 5. I use paginator version 5.8.0 and php 8.0. This is my controller code:

<?php

namespace App\Controller\Frontend;

use App\Controller\BaseController;
use App\Entity\User;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\Routing\Annotation\Route;


class HomepageController extends BaseController
{
    /**
     * @Route("/", name="homepage")
     */
    public function homepageAction(PaginatorInterface $paginator)
    {
        $users = $this->getDoctrine()->getRepository(User::class)->getUsers();

        $pagination = $paginator->paginate([
            $users,
            1,
            1
        ]);

        return $this->render('frontend/homepage.html.twig',
        [
            'users' => $pagination
        ]);
    }


}

And this is my query:

public function getUsers()
    {
        return $this->createQueryBuilder('u')
            ->select('u.email', 'u.id')
            ->orderBy('u.createdAt', 'DESC')
            ->getQuery()
        ;

    }

When I try to access user.id in a twig or anywhere else I get the error:

Neither the property "id" nor one of the methods "id()", "getid()"/"isid()"/"hasid()" or "__call()" exist and have public access in class "Doctrine\ORM\Query".

Has anyone had this issue or has any idea how to fix it? Thank you

I tried changing package version but nothing changes.

0

There are 0 best solutions below