On several occasions now I have been confronted with an issue during a ‘search’ scenario using a CActiveDataProvider and a CGridView:
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]A MySQL table has the following field:[/size][/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]
distance decimal(4,1) NOT NULL DEFAULT '5.0'
[/size][/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]I define a dataProvider for a CGridView like this:[/size][/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]
$filter = new Area('search');
if(isset($_GET['Area'])) {
$filter->attributes=$_GET['Area'];
}
$dataProvider=$filter->search();
[/size][/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]The result is that the search takes into account the default value found by database inspection and the MySQL search includes the following where condition:[/size][/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"] [/font][/color]
WHERE distance LIKE :ycp0. Bound with :ycp0='%5.0%
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"] [/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]Removing the default value from the database "fixes" this (removes the side effect).[/size][/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]For me this is (at the limit of) qualifying as a bug, so I filed an issue in the issue tracker[/size][/font][/color]
[color="#333333"][font=“Helvetica, arial, freesans, clean, sans-serif”][size=“4”]Because the usual method implies the instantiation of a CActiveRecord following the ‘search’ scenario, I claim that the default values found in the database for the respective fields should not be used as default search values.[/size][/font][/color]
[color="#333333"][font=“Helvetica, arial, freesans, clean, sans-serif”][size=“4”]I claim that this is the standard method: in SQL the default value is only used when ‘inserting’ a new record, not when ‘selecting’ or when ‘updating’ a record. Therefore, when the CActiveRecord is instantiated using the ‘search’ scenario, it should not use the default values as search conditions.[/size][/font][/color]
[color="#333333"][font=“Helvetica, arial, freesans, clean, sans-serif”][size=“4”]The claimed standard method under the Yii framework is to use 'unsetAttributes() ’ which inserts in the above code like this:[/size][/font][/color]
[color="#333333"][font="Helvetica, arial, freesans, clean, sans-serif"][size="4"]
$filter = new Area('search');
$filter->unsetAttributes();
if(isset($_GET['Area'])) {
$filter->attributes=$_GET['Area'];
}
$dataProvider=$filter->search();
This likely has been voted on the forums to be the standard method, but I can’t find the reference to that.
The use of ‘unsetAttributes’ would need to be systematic in my humble opinion and therefore done automatically under the ‘search’ scenario.
Please leave a note to share which method you prefer - do you have a case where the current default method is desireable (i.e., the default search method should use the default value defined in the database).
[/size][/font][/color]