Searching data

From FuseWiki

Jump to: navigation, search

Contents

[edit] Introduction

FUSE offers a way to quickly create custom search pages for your data. Rather than having to tediously parse incoming POST or GET data and append it to a growing query string, you can make use of the FuseDataController's builtin search() methods, discussed below


[edit] Example: A Basic Product Search

[edit] ProductController.class.php

FUSE::Require_class('AppControl/FuseDataController');
class ProductController extends FuseDataController {

 
 public $search_fields = array( 
  'product_title' => array('filter_type' => 'like' ),
  'product_number' => array('filter_type' => 'direct' )
 );

}

Above, we have a basic $search_fields array. This is an array of form input names, each of which has its own array that tells the search how it should treat the field. Typically, the only key you will need in the array is filter_type, which is discussed below.


[edit] views/Product/Product-Search.tmpl

<form action="?" method="get">
<input type="hidden" name="start_search" value="1" />

Product Title: 
<{HTML/SearchHelper::text_field( 'Product', 'title' )}>

Product Number:
<{HTML/SearchHelper::text_field( 'Product', 'number' )}>

<input type="submit" value="Search" />
</form>

<{IF search_is_active}>
<table>
  <tr>
    <th>Title</th>
    <th>Description</th>
   </tr>
  <{ITERATOR products}>
  <tr>
       <td style="width: 140px;"><{title}></td>
     <td><{description }></td>
  </tr>
  <{/ITERATOR}>
</table>
<{/IF}>




Two important things to note above:

1. Be sure to include the "start_search" input in your search form - this having a nonzero value tells the search to start over with the new criteria

2. The search_is_active parameter will be set to one if the user has already submitted the search form data.



[edit] Filter types

Supported options for the filter_type option in $search_fields are:

  • fulltext
  • greater_than
  • greater_than_equal
  • in
  • less_than
  • direct (will search using a direct comparison, e.g. myfield=34)
  • like (default - where myfield like '%value%')
  • like_insensitive (case insensitive version of like)
  • wildcard_left (myfield like '%value')
  • wildcard_left_i (myfield like '%value' - case insensitive)
  • wildcard_right (myfield like 'value%')
  • wildcard_right_i (myfield like 'value%' - case insensitive)
  • date_interval (expects dropdowns called search_mydatefield_month_to, _day_to, year_to, _day_from, _month_from, _year_from)


[edit] Generating search inputs

The SearchHelper class contains the following methods.

[edit] SearchHelper Methods

  • text_field()
  • text_area()
  • Select_all()
  • Select_by_method()

Note that these methods all take the same parameters as their counterparts in FormHelper and FormOptionsHelper, which are discussed in: Form Basics and Form Selections.


[edit] SearchHelper Examples

<{HTML/SearchHelper::Select_all('Factory', 'name', 'id', array('empty_value_text' => '-Any-') );}>
<{HTML/SearchHelper::text_field( 'Product', 'number' )}>


[edit] Custom search inputs

You can easily create your own searchable inputs simply by prefixing the input name with "search_" and making sure to add the input name (minus the "search_" prefix) to $search fields. For example, a valid textbox for searching product number would look like:

<input type="text" name="search_product_number" />

[edit] Advanced options

[edit] Changing the database field name

The $search_fields array can also take an option called db_field, which will translate the given input name to a different field name before generating the query. For instance, if you want "search_product_number" to become "products.product_num_internal" instead of the default ("product_number"), you would do the following in your controller:

 public $search_fields = array( 
  'product_title' => array('filter_type' => 'like' ),
  'product_number' => array('filter_type' => 'direct', 'db_field' => 'products.product_num_internal )
 );


[edit] Joining other tables when searching

Sometimes, if a certain field is being searched, you will need to join other tables alongside your data in order to search effectively. For example, a manufacturing company may have customers who use their own internal numbers to identify the same product from the factory. Below is an example of joining the "customer_assignments" table if the "product_customer_number" field is being searched:

       // 
       // This goes in ProductController.class.php 
       //We need to join the customer_assignments table, but only
       // if a product_customer_number was specified 
      //

 public $search_settings = array( 
   'join_if' => array( 'product_customer_number' => 'LEFT JOIN customer_assignments ON customer_assignments.product_id = products.product_id')
 
Personal tools