Use of default values in 'search' scnario

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]

Hi le_top,

Personally I don’t see anything uncomfortable with the current implementation.

IMHO, it’s so-called ‘BY DESIGN’, not a bug at all.

You should note that search() method and ‘search’ scenario are not in the Yii core.

There’s no method like CActiveRecord::search() or a constant like CActiveRecord::SCENARIO_SEARCH.

They belong to a CActiveRecord derived class which you have made.

Of course I know that it was the model generator of gii that created search() method and ‘search’ scenario in your model class. But it’s just a template, or an example. You could have made your own class that extends CActiveRecord with neither search() method nor ‘search’ scenario.

But, yes, I agree with you that the documentation might be insufficient in this area. :)

GitHub discussion about this - https://github.com/yiisoft/yii/issues/804

Here is one suggestion - http://www.yiiframework.com/forum/index.php/topic/10424-

And here is the discussion that we did before implementing unsetAttributes() - http://www.yiiframework.com/forum/index.php/topic/9752-