【发布时间】:2020-08-04 12:28:21
【问题描述】:
我是初学者,我尝试使用 kpn_paginator 为搜索输入编写查询,但出现错误:
其中一个听众必须对给定的目标进行计数和切片
我找不到解决方案,我检查了一些其他主题,但无法理解如何在我的代码中执行此操作。
我的 else 运行良好,但我的 if 不行。
当我尝试通过 (->getResult) 更改我的查询 (->getOneOrNullResult) 时,我收到此错误:
App\Repository\ArticleRepository::findByName() 的返回值必须是 App\Entity\Article 的实例或 null,返回数组
如果你能帮助我,非常感谢。
index.html.twig
<div class="search">
<form action="" method="get" >
<input type="text" name="recherche" placeholder="Titre">
<button type="submit" class="btn btn-primary btn-sm">Recherche</button>
</form>
</div>
控制器.php
/**
* @Route("/", name="admin_article_index", methods={"GET"})
*/
public function index(ArticleRepository $articleRepository, PaginatorInterface $paginator, Request $request): Response
{
$this->articleRepository = $articleRepository;
$recherche = [];
if (isset($_GET['recherche'])) {
$recherche = htmlspecialchars($_GET['recherche']);
$article = $paginator->paginate(
$this->articleRepository->findByName($recherche),
$request->query->getInt('page', 1),
2
);
}else {
$article = $paginator->paginate(
$this->articleRepository->findAllArticleQuery(),
$request->query->getInt('page', 1),
2
);
}
return $this->render('admin/article/index.html.twig', [
'articles' => $article,
]);
}
ArticleRepository.php
public function findByName($recherche): ?Article
{
return $this->createQueryBuilder('a')
->andWhere('a.title = :val')
->setParameter('val', $recherche)
->getQuery()
->getOneOrNullResult()
;
}
【问题讨论】: