Searching Models Based On A Particular Criteria

I have the following in my model

	public function search()


		// Warning: Please modify the following code to remove attributes that

		// should not be searched.

		$criteria=new CDbCriteria;


[b]		$criteria->compare('year',2013);[/b]




		return new CActiveDataProvider(get_class($this), array(







I changed this line




in order to parse for this current year only… now, I would like to be able to call this data set for other years…

what would be the proper way to set up my model in order for that to happen?

it works as is but I can only access 2013 data this way



Have you store a

date (11-07-2013)

or only


in your DB?

You need to go back to your previous solution and if You like change $this->year you just need to set it in the controller.

I think you should stuck with suggestion provided by mirunho

just stored as an integer for the four digit year,i.e 2013


you can write a query on search function

if (isset($this->year) && !empty($this->year)) {

//write a between query here....


hope it may be helpful…

Hey Pabs,

There is nothing wrong here.What you have to do is set this value dynamically from your controller action and keep that line as it is.Like this is model search method


So, the only thing you need to do in controller action is.

$model = new YOURMODEL('search');

$model->year = "2011"; //Or you can set it dynamically. 

So, when you will access


Then it will return records from 2011 years.

i hope it gives you a clear picture.

And if you what to find out the results between two years. Then you have to write a between condition.

$criteria->addBetweenCondition('year','2011', '2012');

thanks guys

I ended up doing as you suggested

kept my model with this

		$criteria->compare('year',$this->year);  //this is hack!  must be a clea way of  doing this

set the year var in my controller

	public function actionIndex()


		$model->year = date("Y");


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

		'pagination' => array(

            'pageSize' => 100,










works as it should…still getting used to this concept of controller/model :)

thats great ;)