简体   繁体   English

Symfony knp_paginator 查询 $_GET

[英]Symfony knp_paginator query with $_GET

I am a beginner and I try to write a query for a search input using kpn_paginator but I get an error:我是初学者,我尝试使用kpn_paginator为搜索输入编写查询,但出现错误:

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:当我尝试通过 (->getResult) 更改我的查询 (->getOneOrNullResult) 时,出现此错误:

Return value of App\Repository\ArticleRepository::findByName() must be an instance of App\Entity\Article or null, array returned App\Repository\ArticleRepository::findByName()的返回值必须是App\Entity\Article的实例或者null,返回数组

If you can help me thank you very much.如果你能帮助我,非常感谢。

index.html.twig 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>

Controller.php Controller.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 ArticleRepository.php

public function findByName($recherche): ?Article
    {
        return $this->createQueryBuilder('a')
            ->andWhere('a.title = :val')
            ->setParameter('val', $recherche)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }

Must to add join:必须添加加入:

/**
 * @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');
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM