2016-06-20 1 views
-3

Я хочу добавить композицию панели поиска, чтобы отфильтровать результаты поиска. На самом деле у меня есть только функция, которая дает мне все данные из БД.Как добавить «панель поиска» или funtion в приложении 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

спасибо, что может быть полезно – firasKoubaa

Смежные вопросы