2014-02-07 211 views
-1
public function searchAction(Request $request) 
{  
    $em = $this->getDoctrine()->getEntityManager(); 
    $searchName = $request->query->get('search_name'); 
    $searches= \explode(' ', $searchName); 
    $search = $em->createQueryBuilder()->select('c') 
        ->from('SavvyContactBundle:Contact', 'c') 
        ->where("c.name = :name") 
        ->setParameter('name', $searchName) 
        ->addOrderBy('c.id', 'DESC') 
        ->getQuery(); 

    return $this->render('SavvyContactBundle:Contact:list.html.twig', array('search' => $search, 'searches' => $searches)); 
} 

這是特定的單詞基本的搜索,我想做出這樣的搜索,其估算在輸入欄,請幫助,讓我知道如果我錯了地方的每一個字高級搜索。高級搜索

+0

請問您能給我一些反饋嗎? – coma

+0

對不起,我忘了你,但你的代碼工作謝謝 – craig

回答

0

我正在寫手機,但你明白了嗎?

... 

$words = explode(' ', $searchName); 

$qb = $em->createQueryBuilder() 
    ->select('c') 
    ->from('SavvyContactBundle:Contact', 'c') 
    ->addOrderBy('c.id', 'DESC'); 

foreach ($words as $word) { 

    $qb 
     ->orWhere("c.name = :word") 
     ->setParameter('word', $word); 
} 

$contacts = $qb->getQuery()->execute(); 

...