symfonyfilterknppaginator

Simple filter in symfony when query and controller already exists


I have some confusion on how to create a filter with a query and a controller that already exists (with KnpPaginator in it)

I have in my repo a query that search through something, and in my controller two functions private called resultsAction and a public one called offreAction that are both together.

Any idea on how to create a simple filter? I followed this tutorial but my query already exists in my repo and it didn't work when I applied it.

My repo

public function getQueryByTypeAndPro($type, User $user, $archive)
{
    return $this->createQueryBuilder("opn")
        ->andWhere("opn.type = :type")
        ->setParameter("type", $type)
        ->andWhere("opn.resellerId = :reseller")
        ->setParameter("reseller", $user->getId())
        ->andWhere("opn.archive = :archive")
        ->setParameter('archive', $archive)
        ->orderBy("opn.dateCreation", "DESC")
        ->getQuery()
    ;
}

my controller

private function resultsAction(Request $request, User $user, $type, $archive)
    {
        $em = $this->getDoctrine()->getManager();

        $paginator = $this->get('knp_paginator');

        $qb = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive);

        $results = $paginator->paginate(
            $qb,
            $request->query->get('page',1),
            $request->query->get('limit',50),
            [
                'defaultSortFieldName'      => 'opn.dateCreation',
                'defaultSortDirection' => 'desc'
            ]
        );

        return array("results" => $results, "archive" => $archive);
    }

public function offreAction(Request $request, User $user, $archive = false)
    {
        return $this->resultsAction($request, $user, Operation::OFFRE_COMMERCIALE, $archive);
    }

Should I create a new function (public) in my controller in order to make that work? or can I do it directly in resultsAction where KnpPaginator is ?

Thank you


Solution

  • I fixed my problem. Here is the answer

    Here is my query where I added the filter

    public function getQueryByTypeAndPro($type, User $user, $archive, $filter)
    {
        return $this->createQueryBuilder("opn")
            ->andWhere("opn.type = :type")
            ->setParameter("type", $type)
            ->andWhere("opn.resellerId = :reseller")
            ->setParameter("reseller", $user->getId())
            ->andWhere("opn.archive = :archive")
            ->setParameter('archive', $archive)
            ->andWhere('opn.vehiculeMarque like :vehiculeMarque')
            ->setParameter('vehiculeMarque', '%' . $filter . '%')
            ->andWhere('opn.id like :id')
            ->setParameter('id', '%' . $filter . '%')
            ->orderBy("opn.dateCreation", "DESC")
            ->getQuery()
        ;
    }
    

    Here is how I use it in the controller

    $qb = $em->getRepository("STUserBundle:Operation")->getQueryByTypeAndPro($type, $user, $archive, $filter);
    

    and here is how i use it in my view

    <form action="" method="get">
             <input name="filter" type="text">
             <button type="submit" class="btn btn-default">Filtrer</button>
    </form>
    

    hope that helps