2016-06-20 98 views
-3

我想添加一个搜索栏组合函数来过滤搜索结果。其实我只是一个函数,它给了我所有来自数据库的数据。如何在Symfony 2应用程序中添加“搜索栏”包或功能

public function indexAction() { 
    $em = $this->getDoctrine()->getManager(); 

    $entities = $em->getRepository('MaisonMaisonBundle:Maison')->findAll(); 

    return array(
     'entities' => $entities, 
    ); 
} 

我想添加一个SQL请求,它给了我一个筛选结果,甚至是一个搜索栏包。

回答

0

您提供的信息非常少。我仍然会尽力帮助你。

使用Doctrine Query Builder

例如;

// 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, 
    ); 
} 
+0

感谢一个可能有用的ot – firasKoubaa

相关问题