开发者

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!


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 :)


I finally made a custom form using the default MyModuleForm generated by Symfony

public function executeIndex {
    ...
    // Add a form to filter results
    $this->form = new MyModuleForm();
}

but displaying only a custom field:

<div id="search_box">
    <input type="text" name="criteria" id="search_box_criteria" value="Search..." />
    <?php echo link_to('Search', '@my_module_search?criteria=') ?>
</div>

Then I created a route named @my_module_search linked to the index action:

my_module_search:
  url:          my_module/search/:criteria
  param:        { module: my_module, action: index }
  requirements: { criteria: .* }  # Terms are optional, show all by default

With Javascript (jQuery in this case) I append the text entered to the criteria parameter in the href attribute of the link:

$('#search_box a').click(function(){
    $(this).attr('href', $(this).attr('href') + $(this).prev().val());
});

And finally, back to the executeIndex action, I detect if text was entered and add custom filters to the DoctrineQuery object:

public function executeIndex {
    ...
    // Deal with search criteria
    if ( $text = $request->getParameter('criteria') ) {
        $query = $this->pager->getQuery()
            ->where("MyTable.name LIKE ?", "%$text%")
            ->orWhere("MyTable.remarks LIKE ?", "%$text%")
            ...;
    }

    $this->pager->setQuery($query);

    ...
    // Add a form to filter results
    $this->form = new MyModuleForm();
}

Actually, the code is more complex, because I wrote some partials and some methods in parent classes to reuse code. But this is the best I can came up with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜