What is the best way to handle complex searches?


I have a list of products that I allow the user to filter using a numeric size or text description.

I also can show the same list if they are on sale or a user clicks on a category to narrow the list.

If the user uses the numeric size, I show a list of the categories before the product list and allow the user to click on a category to narrow the selection.

currently I am using sessions to store category clicked, onsale clicked, numeric or text entered.

in the controller, I just build the sql in the actionIndex.

My question is, what is the best way to handle searching when the values can come from links or numeric or text entry that must be kept and allow the user to drill down by category?

  1. Do I have an action for numeric entry, another for category clicked, another for onsale clicked.

  2. Do I use sessions or setState to preserve filters?

I feel I am making this much harder than it needs to be.

If this is not clear, I can give some code examples.



Hi Frank,

If you need to preserve the filters in all the session, I suggest to use setState function.

But, if it is not a must, you can use GET variables.

Some examples could help :)

Best regards.

Thanks, I tried setState and that seems to work ok.

$search = Yii::app()->user->getState('search');	// from textField

$category = Yii::app()->user->getState('category'); // from link click

$onsale = Yii::app()->user->getState('onsale'); // from link click

$product_sort = Yii::app()->user->getState('product_sort','price');

$dataCategories = null;

if (is_numeric($search))

   $dataCategories = Category::model()->getCategories($category, $search, $onsale); 


// Build sql

$c = is_numeric($search) ? 'Sizenum like :p1 and price > 0' : 'Item_description like :p1 and price > 0';

$c .= ' and On_Hand_qty > 0 and Istatus="A" ';


if (!empty($category)) $c .= ' and Category_id = "' . $category .'"';

if (!empty($onsale)) $c .= ' and onsale = "Y"';

$dataProvider=new CActiveDataProvider('Product', array(


	'select'=>'price, Item_description, On_hand_qty, Item, onsale',