I think there must be a solution without removing this feature from AR. For example search scenario sets default values to empty strings. Or adding optional $castDefaultValues to AR constructor. (not sure how logic that is, didn’t used the filtering and grid-stuff much)
I don’t know how it works, but wouldn’t it be possible to execute such a db expression and then use the returned value as default?
I have not tried it myself, but I don’t see why we can’t combine this with CDbExpression like so :
public function rules()
array('fieldthatNeedsDefault', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => true),
In theory, this will set your model field to the CDbExpression only if another value was not given. I can vouch that it works with a plain value, and while I haven’t tested it with a CDbExpression, I don’t see why it would not work. It is at least worth a try. Just remember that you have to use validate() on your model to make the rules kick in, but after that you should be good to go!
Using default values from DB feels obscure for me. I always assign default values in the model, as this is more transparent for me and gives a better understanding of what’s going on. As it doesn’t work for all DBMS anyway i consider this feature untrustworthy and would never rely on it. But some users might think that, and could report bugs if not all default values are used. We shouldn’t give “false reliability”
On the other hand i guess, we could still assign default values in the model class and thus override the default values from DB. As long as this is guaranteed, it doesn’t matter for me.
I think that the method to initialize should be in the core, it’s default implementation is to load the defaults from the db, and called automatically when the model is created, so BC is not a problem…
BUT, this method shoul be overwriten by the code generator in next version of yii (e.g.: 1.4 and +) for each model…
I really like the idea of having Gii set the defaults at model generation.
Would be nice if you could combine that with a toggle to load the defaults or not - if Gii generated with defaults, then have gii generate the toggle set to false, otherwise let the default toggle setting be true (load defaults).