Help understanding filters, add_filter(), and how to perform advanced module search.

edited October 2011 in Feature Requests
Hello there,

I'm having a hard time figuring out how to use add_filter() to return an advanced module's model records based on a search term. I looked at some of fuel's builtin modules but i still have some questions.

Let's say i want to search for an "article" that has the word 'test' in either the title or its content.

1) Where should i add these filters so that the returned results of find_all only include the search results? Inside the module itself or can i call the
$this->module_name->add_filters() 
function from a controller?
2) How should i apply these filters? I tried doing
$this->module_name->add_filters(array('title' => 'test', 'content' => 'test')) 
but i still get ALL results in the following find_all()

I guess I need a simple example with a module being loaded and searched with a static term. I will have no problems adding the form handling to that then...

Comments

  • edited 8:28PM
    You can add a property to your model called $filters which will tell it which fields to search in automatically:
    public $filters = array('title', 'content_filtered', 'fuel_users.first_name', 'fuel_users.last_name');
    Additionally, there is a "filter_join" property that you can specify either "and" or "or" which by default is "or". This query is generated in the base_module_model.php list_items() method.
  • edited 8:28PM
    ok but how do i pass the 'test' string to these filters then?
  • edited 8:28PM
    There is a $filter_value property that you can set on the model. This is done in the admin in the fuel/modules/fuel/controllers/module.php items() method.
  • edited 8:28PM
    I believe since pierlo is working on advance module , he should write a controller which extend the module controller, so that he can add the test in items function which override the module controller.
  • edited October 2011
    Ok so here's what i've done:

    1) set
     public $filters = array('title'); 
    in my model class
    2) right before the find_all() method , i set
    $this->filter_value = 'test';
    UPDATE:
    this works for list_items but not in my custom get_all() method that uses find_all(), - I'll go ahead and see how to add the filters to find_all()
  • edited 8:28PM
    Seems like i'm better off using list_items to get filtered results anyway. Thanks for the help!
  • edited 8:28PM
    That's correct, it is specific to the base_module_model and is used for the list_items method.
  • edited 8:28PM
    Yippee!
Sign In or Register to comment.