yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

loadDefaultValues, NOT NULL on TIMESTAMP column MySql #9759

Open andrewblake1 opened 9 years ago

andrewblake1 commented 9 years ago

Not sure if the following is desired behaviour:

Because MySql unfortunately automatically adds a default of CURRENT_TIMESTAMP to the first timestamp column of a table (even when NOT NULL attribute is set) this comes through as the value text set for the attribute in loadDefaultValues.

While it is annoying behaviour from MySql it is the documented action for first TIMESTAMP column.

In one situation I found, I was wondering why a required rule wasn't being enforced on a DatePicker via kartik-v\DateControl. It was because with DateControl the submitted input is hidden and was carrying a value of "CURRENT_TIMESTAMP".

Have overridden in loadDevaultValues and tested for this in my code but thought I would raise in case it was undesired in general.

nineinchnick commented 9 years ago

Core issue here is there is no support for sql expressions as default values, only scalar constants.

klimov-paul commented 9 years ago

Same as #7484

nineinchnick commented 9 years ago

@klimov-paul it's not the same issue. It's about using the default value from the db, not assigning an expression in Yii.

klimov-paul commented 9 years ago

So what is your suggestion then? Insert the record - get the generated value and then delete the record? This is the only way the get such values. PHP can not evaluate SQL expressions.

klimov-paul commented 9 years ago

One way or another there is nothing we can do about this issue.

nineinchnick commented 9 years ago

Yii should properly detect sql expression and either ignore them when calling loadDefaultValues() or put them in \yii\db\Expression, not treat them as a string value.

nineinchnick commented 9 years ago

Actually, it should load them as \yii\db\Expression. A use case for that is the application could display a hint in a form that there is a default value and the user can leave the field empty. It should not be evaluated, because the expression might reference other columns or call functions that make sense only at the moment of INSERT.

klimov-paul commented 9 years ago

If you pass \yii\db\Expression instance into a form it will produce a mess.

Still, this may be a working solution.