2016-06-20 35 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

相關問題