我在树枝循环中使用分页器束时出错,我认为missions
正在等待sliddingpagination
而不是persistentcollection
,我认为它在FindAllVisibleQuery
的控制器中,就像等待数组但 IDK。
这是我收到的错误:
参数 2 传递给 knp\Bundle\PaginatorBundle\Twig\Extension\PaginationExtension::render(( 必须是 的实例 Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface, 给定数组,调用/Applications/MAMP/htdocs/MySUPER/var/cache/dev/twig/12/12cef122792496c7b8dbcf0cc1f03b575f10f859de41093d7a8fd223b1623d7d.php 在第 176 行
任务控制器.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'
]);
}
}
?>
树枝片段
{% 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 %}
任务存储库.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()
;
}
*/
}
您需要编辑存储库类以返回QueryBuilder而不是查询本身。
你的函数必须是:
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;
}