Skip to content

Instantly share code, notes, and snippets.

@MintayRibkins
Created February 1, 2019 20:26
Show Gist options
  • Save MintayRibkins/8b69aec4155a7258f5124714b3b0a402 to your computer and use it in GitHub Desktop.
Save MintayRibkins/8b69aec4155a7258f5124714b3b0a402 to your computer and use it in GitHub Desktop.
public function productSearchAction(Request $request)
{
$q = $request->query->get('q');
$q = preg_split('/\s+/', $q);
$dql = array();
$em = $this->getDoctrine()->getManager();
$search = $em->getRepository('DmitroShopBundle:Product');
$qb = $search->createQueryBuilder('prod');
foreach ($q as $i => $_q) {
$dql[] = "prod.name LIKE :q$i";
$qb->setParameter("q$i", '%' . $_q . '%');
}
$qb->andwhere('(' . implode(' OR ', $dql) . ')');
$adapter = new DoctrineORMAdapter($qb, false);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(20);
$pagerfanta->setCurrentPage($request->query->get('page', 1));
if (!$pagerfanta->getNbResults()) {
throw $this->createNotFoundException('Ci dispiace, ma il prodotto non è stato trovato'. implode(" ", $q));
}
return $this->render(
'product/listProduct.html.twig',
array('productsPagerfanta' => $pagerfanta)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment