firasKoubaa firasKoubaa - 1 year ago 139
PHP Question

How to add a "search bar " bundle or funtion in a Symfony 2 app

i wanna add a search bar composant to filter my results of searching, actually i ve just a fonction wihich give me all data from DB

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

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

return array(
'entities' => $entities,

i want to just add an sql request which gives me a filtered result , even a search bar bundle.

any ideas ?????

Answer Source

You have given very little information. Still i will try to help you.

Use Doctrine Query Builder.

For example;

// 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();

        ->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,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download