Php 如何添加一个“;搜索栏“;Symfony 2应用程序中的捆绑或功能
我想添加一个搜索栏组件来过滤我的搜索结果。实际上,我只有一个函数,它提供了数据库中的所有数据Php 如何添加一个“;搜索栏“;Symfony 2应用程序中的捆绑或功能,php,mysql,symfony,twig,Php,Mysql,Symfony,Twig,我想添加一个搜索栏组件来过滤我的搜索结果。实际上,我只有一个函数,它提供了数据库中的所有数据 public function indexAction() { $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository('MaisonMaisonBundle:Maison')->findAll(); return array( 'entities
public function indexAction() {
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('MaisonMaisonBundle:Maison')->findAll();
return array(
'entities' => $entities,
);
}
我只想添加一个SQL请求,它会给我一个过滤结果,甚至是一个搜索栏包。您提供的信息很少。我还是会尽力帮助你 使用 比如,
// Add this after your namespace
use Symfony\Component\HttpFoundation\Request;
// Modify your indexAction like below
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
// call for the query builder
$qb = $em->createQueryBuilder();
$qb
->select('m')
->from('MaisonMaisonBundle:Maison', 'm')
->where($qb->expr()->like('m.title', ':title'))
->setParameter('title', '%'.$request->get('keyword').'%'); // use $request->request->get('keyword') if you are using post method instead get with the form.
$entities = $qb->getQuery()->getResult();
//$entities = $em->getRepository('MaisonMaisonBundle:Maison')->findAll();
return array(
'entities' => $entities,
);
}
谢谢,这可能有用