Couple of queries regarding CGridFiew filters

  1. If I enter a value in one of the filter columns and then tab away - the filter is applied. I then use the pager to go to another page - again works fine. I then change the filter value and tab away - but the original filter value remains.

Is this the expected behaviour or is it a bug?

  1. Does anyone know how to search on a datetime field using the filters? I’m displaying the date as follows:



public function getRegisterDate()

{

	return date('d M Y H:i', strtotime($this->register_date));

}



Returns: 10 Oct 2010 10:00

So I want to be able to search the date in that particular format, however it will currently only allow me to search if I put in the MYSQL DATETIME format. I have tried setting the ‘compare’ value for this attribute in the search() function but I cannot get it to work as intended.

1 - not expected and confirmed as a possible problem… please add an issue - http://code.google.com/p/yii/issues/entry

2 - the search is done in the $model->search() method… there you can elaborate the entered value as you need it and perform the search… for now I think you have just $criteria->compare(‘date field’,‘entered date value’) and that’s why it filters only if you enter mysql date…

Cheers, I’ve logged the issue.

OK I’ve tried the following:


$criteria->compare('register_date', date('d M Y H:i', strtotime($this->register_date)));

And one with just strtotime, but none of them seem to work.

You need to convert the user entered date to the database format… so that a SELECT query can find what you need…

OK cheers - just the last hurdle now. I did the following in my actionAdmin:


if(isset($_GET['User']))

{

	$model->attributes=$_GET['User'];

	$model->register_date=date('Y-m-d H:i:s', strtotime($model->register_date));

}

And this works fine for me, in that it filters out the records. However when tabbing away from the filter field the date gets displayed in its MYSQL DATETIME format - any way to make it retain the value the user inputted?

You can do something like




$tmpVar=date('Y-m-d H:i:s', strtotime($model->register_date));

$criteria->compare('register_date',$tmpVar);



This way you leave the original entered value as it is…

I already tried that. The grid just disappears on page load.

It’s possible that the compare is outside the if()… so that $tmpVar has no value…




if(isset($_GET['User']))

{

	$tmpVar=date('Y-m-d H:i:s', strtotime($model->register_date));

	$criteria->compare('register_date',$tmpVar);

}



Cheers mate, that has worked ;)

If I enter a date and then tab away the results are filtered correctly, now if I then clear out the date and tab away I get no results.

EDIT: This works:


if(!empty($this->register_date))

{

	$criteria->compare('register_date', date('Y-m-d H:i:s', strtotime($this->register_date)));

}

else

{

	$criteria->compare('register_date', $this->register_date); 

}