Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a search box to filter a list of results in Symfony?

I need to put a search box within a list of objects as a result of a typical indexSuccess action in Symfony. The goal is simple: filter the list according to a criteria.

I've been reading the Zend Lucene approach in Jobeet tutorial, but it seems like using a sledge-hammer to crack a nut (at least for my requirements).

I'm more interested in the auto-generated admin filter forms but I don't know how to implement it in a frontend.

I could simply pass the search box content to the action and build a custom query, but is there any better way to do this?

EDIT

I forgot to mention that I would like to have a single generic input field instead of an input field for each model attribute.

Thanks!

like image 861
elitalon Avatar asked Dec 02 '25 16:12

elitalon


1 Answers

I'm using this solution, instead of integrating Zend Lucene I manage to use the autogenerated Symonfy's filters. This is the way i'm doing it:

//module/actions.class.php
public function executeIndex(sfWebRequest $request)
{
      //set the form filter
      $this->searchForm = new EmployeeFormFilter();
      //bind it empty to fetch all data
      $this->searchForm->bind(array());
      //fetch all
      $this->employees = $this->searchForm->getQuery()->execute();
      ...
}

I made a search action which does the search

public function executeSearch(sfWebRequest $request)
{
  //create filter
  $this->searchForm = new EmployeeFormFilter();
  //bind parameter
  $fields = $request->getParameter($this->searchForm->getName());
  //bind
  $this->searchForm->bind($fields);
  //set paginator
  $this->employees = $this->searchForm->getQuery()->execute();
  ...
  //template
  $this->setTemplate("index");
}

It's important that the search form goes to mymodule/search action.

Actually, i'm also using the sfDoctrinePager for paginate setting directly the query that the form generate to get results properly paginated.

If you want to add more fields to the search form check this :)

like image 130
Pabloks Avatar answered Dec 05 '25 00:12

Pabloks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!