2013-03-05

Introduction

After a lot of research, everything I found on adding date range searching to a CGridView advanced search form seemed to involve adding two new public variables (e.g. $date_from, $date_to), 'safe' rules for the new variables, and a rather chunky if/elseif/else check in the search() method. This probably isn't a hassle for most, but because many of the tables in my database contain two or three and sometimes four date columns (e.g. date_created, date_modified, date_deleted etc.), it meant I was having to add up to eight public variables, the corresponding safe rules, and modifying the search() criteria for each date attribute. So, I set about creating a better way and I thought I'd share my work with the community.

Overview

To be able to add date range searching functionality to your advanced search forms (not grid view filters sorry, I'm still working on that!) without having to add public attributes, 'safe' rules, and chunky search conditions for each date attribute.

Create an Active Record Behavior that builds the criteria for the search() method and attach it to your model

Modify the search() method of the model to merge the behavior's criteria in with the rest of the searchable attributes

Modify the _search form view to add the date range inputs

Assumptions

You already have a functioning advanced search form for your grid view that contains one or more date inputs.

Your database date is in the MySQL format 'yyyy-mm-dd'. If not, change the $dateFromDefault and $dateToDefault values in the behavior.

Behavior

Create a file called EDateRangeSearchBehavior.php. Copy the following code in to the file and save it in to your components/behaviors folder.

Model

Then, attach the behavior by adding it to the behaviors() array in your model

Next, modify the search() method in the same model and replace the $criteria->compare() line for every date attribute you want to range search.

View

And finally in your view, add the following code to your _search form to render your 'date from' and 'date to' inputs.

Summary

The View code will generate two search inputs, each having its own CJuiDatePicker widget. The two inputs will submit the data under the attribute name as an array, where the EDateRangeSearchBehavior attached to the model will create a BETWEEN condition.

Obviously, if the two date inputs are filled in, the search will find models between the two entered dates.

If only the first date input is filled in, the search will find models between the entered date and the '$dateToDefault' date set in the behavior.

If only the second date input is filled in, the search will find models between the '$dateFromDefault' date set in the behavior, and the entered date.

If, for example, you have an admin search form and a user search form that use the same model, and you only require date range functionality for the admin search form, you are still able to use a single date input. The behavior checks if the attribute submitted is an array (i.e. a date range), and if not it will revert to a standard 'compare' criteria.

Conclusion

I hope this works well for you. In my case, I actually use this for datetime/timestamp fields, and with some simple modifications (i.e. add the time to the $dateFromDefault and $dateToDefault values in the behavior, and change the view widget to a DateTimePicker) you can adapt it to your needs too. Also, if you have any enhancements, I'd love to hear.

Show more