Argument 2 must be an instance of paginator in symfony

I’m having an error using the paginator bundle inside a twig loop , I think missions is waiting for a sliddingpagination and not a persistentcollection , I think it’s in the controller in the FindAllVisibleQuery, like waiting for an array but IDK..

This is the error I’m receiving:

Argument 2 passed to
KnpBundlePaginatorBundleTwigExtensionPaginationExtension::render()
must be an instance of
KnpBundlePaginatorBundlePaginationSlidingPaginationInterface,
array given, called in
/Applications/MAMP/htdocs/MySUPER/var/cache/dev/twig/12/12cef122792496c7b8dbcf0cc1f03b575f10f859de41093d7a8fd223b1623d7d.php
on line 176

MissionController.php

<?php

namespace AppController;


use AppEntityMission;
use AppEntityMissionSearch;
use AppFormMissionSearchType;
use AppRepositoryMissionRepository;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use KnpComponentPagerPaginatorInterface;
use SymfonyComponentHttpFoundationRequest;

class MissionController extends AbstractController {

    /**
     * @var MissionRepository
     */
    private $repository;
    /**
     * @var EntityManagerInterface
     */
    private $em;

    public function __construct(MissionRepository $repository, EntityManagerInterface $em)
    {
        $this->repository = $repository;
        $this->em = $em;
    }

    /**
     * @return Response
     * @throws TwigErrorLoaderError
     * @throws TwigErrorRuntimeError
     * @throws TwigErrorSyntaxError
     * @Route ("/missions" , name="Missions")
     */
    public function index(PaginatorInterface $paginator, Request $request): Response
    {
        $search = new MissionSearch();
        $form = $this->createForm(MissionSearchType::class, $search);
        $form->handleRequest($request);

        $missions = $paginator->paginate(
            $this->repository->findAllVisibleQuery($search),
            $request->query->getInt('page', 1),
            18
        );
        return $this->render('lesmissions/index.html.twig', [
            'current_menu' => 'missions',
            'missions'   => $missions,
            'form'         => $form->createView()
        ]);
    }


    /**
     * @Route("/missions/{slug}-{id}", name="mission.show", requirements={"slug": "[a-z0-9-]*"})
     * @param Mission $mission
     * @return Response
     */
    public function show(Mission $mission, string $slug): Response
    {
        if ($mission->getSlug() !== $slug) {
            return $this->redirectToRoute('mission.show', [
                'id' => $mission->getId(),
                'slug' => $mission->getSlug()
            ], 301);
        }
        return $this->render('lesmissions/show.html.twig', [
            'mission' => $mission,
            'current_menu' => 'missions'
        ]);
    }


}


?>

Twig snippet

{% extends 'base.html.twig' %}

{% block title 'Missions' %}

{% block body %}
    <div class="jumbotron text-center">
        <h1>Trouvez une mission qui vous correspond</h1>
        <p>My super est encore en developpement , vous pouvez apporter vos idées via le formulaire de contact</p>
    </div>

    <div class="jumbotron">
        <div class="container">
            {% if form is defined %}
            {{ form_start(form) }}
            <div class="form-row align-items-end">
                <div class="col">
                    {{ form_row(form.maxGain) }}
                </div>
                <div class="col">
                    {{ form_row(form.deltaVille) }}
                </div>
                <div class="col">
                    {{ form_row(form.options) }}
                </div>
                <div class="col">
                    <div class="form-group">
                        <button class="btn btn-primary">Rechercher</button>
                    </div>
                </div>
            </div>
            {{ form_end(form) }}
            {% endif %}
        </div>
    </div>

    <div class="container">
        <h2>Les dernieres missions</h2>
        <div class="row flex">

            {% for mission in missions %}
                <div class="col-3">
                    <div class="card mb-4">
                        <div class="card-body">
                            <h5 class="card-title">
                                <a href="{{ path('mission.show', {id: mission.id, slug: mission.slug}) }}">{{ mission.titre }}</a>
                            </h5>
                            <p class="card-text">{{ mission.adresse }} {{ mission.ville }}</p>
                            <div class="text-primary" style="font-weight: bold;font-size: 2rem;">{{ mission.gain }} €</div>
                        </div>
                    </div>
                </div>

                <div class="pagination">
                    {{ knp_pagination_render(missions) }}
                </div>
            {% endfor %}

        </div>
    </div>
{% endblock %}

MissionRepository.php

<?php

namespace AppRepository;

use AppEntityMission;
use AppEntityMissionSearch;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use DoctrineORMQuery;
use DoctrinePersistenceManagerRegistry;
use DoctrineORMQueryBuilder;


/**
 * @method Mission|null find($id, $lockMode = null, $lockVersion = null)
 * @method Mission|null findOneBy(array $criteria, array $orderBy = null)
 * @method Mission[]    findAll()
 * @method Mission[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class MissionRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Mission::class);
    }

    /**
     * @return Query
     */
    public function findAllVisibleQuery(MissionSearch $search): Query
    {
        $query = $this->findVisibleQuery();

        if ($search->getMaxGain()) {
            $query = $query
                ->andWhere('p.price <= :maxgain')
                ->setParameter('maxgain', $search->getMaxGain());
        }

        if ($search->getDeltaVille()) {
            $query = $query
                ->andWhere('m.ville = :deltaville')
                ->setParameter('deltaville', $search->getDeltaVille());
        }

        if ($search->getOptions()->count() > 0) {
            $k = 0;
            foreach($search->getOptions() as $option) {
                $k++;
                $query = $query
                    ->andWhere(":option$k MEMBER OF m.options")
                    ->setParameter("option$k", $option);
            }
        }

        return $query->getQuery();
    }


    /**
     * @return Mission[]
     */
    public function findLatest(): array
    {
        return $this->findVisibleQuery()
            ->setMaxResults(30)
            ->getQuery()
            ->getResult();
    }

    private function findVisibleQuery(): QueryBuilder
    {
        return $this->createQueryBuilder('m')
            ->where('m.Accepted = false');
    }

    // /**
    //  * @return Mission[] Returns an array of Mission objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('m')
            ->andWhere('m.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('m.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Mission
    {
        return $this->createQueryBuilder('m')
            ->andWhere('m.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *