I am a beginner and I try to write a query for a search input using kpn_paginator
but I get an error:
One of listeners must count and slice given target
I can not find the solution, I checked some other topic but cannot understand how to do it in my code.
My else work well but my if not.
When I try to change my query (->getOneOrNullResult) by (->getResult) I get this error:
Return value of AppRepositoryArticleRepository::findByName() must be an instance of AppEntityArticle or null, array returned
If you can help me thank you very much.
index.html.twig
JavaScript
x
<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>
Controller.php
JavaScript
/**
* @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
JavaScript
public function findByName($recherche): ?Article
{
return $this->createQueryBuilder('a')
->andWhere('a.title = :val')
->setParameter('val', $recherche)
->getQuery()
->getOneOrNullResult()
;
}
Advertisement
Answer
Must to add join:
JavaScript
/**
* @return Query
*/
public function findVilleArticleQuery($search)
{
$query = $this->findArticleQuery();
if ($search = $_GET["villesearch"]){
$query = $query
->join('a.localiser', 'l')
->where('l.town LIKE :ville')
->setParameter('ville', '%' . $search . '%' )
;
}
return $query->getQuery();
}
private function findArticleQuery()
{
return $this->createQueryBuilder('a')
->orderby('a.created_at','DESC');
}