Discussion - Search building.

Hi, i'm new to Yii and is currently trying out this framework for a freelance project i am working on so far.

I've looked around the forums and documentations and found not much information about building search functionalities into your applications.

In your opinions, what would be the cleanest way to build in a search function for the list view of a particular model? i.e. my project is currently a used-car catalog site, so i would want to be able to search my list of cars with a multi field form with these fields:

  • Search Term
  • Make
  • Model
  • Engine Capacity
  • Etc.

My first thought would be to build a new Search Action into the Car controller and have it render the list. But i'm not sure the search will be persistent that way (i.e. after clicking on pagination links, will the listings still be relevant to the search or will it reset to the full list?)

Appreciate any responses.

You can create a SearchForm by extending CFormModel and use it to hold the search criteria (e.g. keyword, make, model) that the user enters.

You then use build a HTML form which should submit in GET mode.

The following is the possible action code:


	public function actionSearch()


		$form=new SearchForm;

		$pages=new CPagination;














I will try this.

I am curious though, why do we not modify the Controller of the model we are searching in instead?

How would you like to modify the controller? I'm not sure if I understand your question…


What i mean is to add the search action into the Controller of the model i wish to search (i.e. car). and also add public attributes to this controller for the search fields as well.

The reason i want to do this is so i can have a search in both admin and list views.

The search action code I gave IS in the controller. The reason for storing search fields in a model class instead of controller is to abide to MVC.


My bad. I kept thinking for some reason that you meant to create a SearchFormController for the SearchForm model which i felt was unnecessary and counter-productive

This seems like a clean solution, i'll try to see what i can write up with this.

Once again thanks alot. This is a great framework.