AR default values

Hi!

I am back again to the defaults values.

Does ‘allowEmpty’ mean 0 for integers?

What if I had in my $model rules ‘allowEmpty’=>true, and AR will turn the NULL into 0 , do I have to add ‘default’, ‘setOnEmpty’=>true, ‘value’=null to every field that should be null by default?

Seems like to say "Hey this NULL is NULL! It could not be 0!!!"

But if I do not say so - the NULL turns into 0 just like water into wine…

Please give me a clear answer! Do I have to specify EVERY default value by setting ‘default’ rule and ignoring what I specified in DB table?

Praying for mercy (by default :rolleyes:)

Thank you Gods!

P.S.

Now serious:

At first my answer was "Yes, we should initialize AR with default attribute values as specified in DB". But seeing how the Yii project developing I could say, that in the future Yii will do everything: No DB editor needed, no raw JavaScript (some already not knowing what it is), no raw HTML, no raw PHP as it was, no raw SQL. Just pure Yii programming with your favourite PHP (read Yii editor) at server side. Is this what to expect? Than Yii should set the rules for DB engines… Not to create ARs from DB tables, but create tables from ARs on application start up according to the rules of the certain AR may be? Just a thought…

If it possible to make an option for AR to retrieve defaults from DB or not. Then why not to have global option in config?

Or I missed something?

As for the search. I have several setSearchDefaults() for different cases…

Should be a Global option I think.

And Gii could generate it based on that one.

[s]Why break the BC? Wouldn’t be better to allow default initialization or not on the main.php config file or any of the other mentioned ways?

I understand when there is a major step (Yii 2.0) but not supporting BC at this stage, personally I don’t think is good idea.[/s]

Didnt see the dates of this post… gosh!

I think that how this functionality is implemented in Yii is a good solution.

In create we will find the default value already set in the textbox, and that is the expected behavior, if we set some default in the database.

For search you have to call $model->unsetAttributes(), this is not an huge price to pay (i fact, it generates gii by default).

I see no reasons to initalize it by default values, imho: it’s only overhead.

I don’t see why you see this as a problem…

In your case you don’t need to set a default value for fields in the database… Yii will not set anything by himself ;)

Is this the reason why my select box defaults to “Yes” when I have the default in my database set to “1”? If yes, then I have to vote yes since it actually saves me a lot of effort in re-declaring the default values that I have in my database to my codes. Why would I want my database defaults and codes to inconsistent right? And why should I declare the defaults in my database design if I don’t want it to be the real default to be encountered by the users? The problem indicated in http://code.google.c.../detail?id=1050 was already fixed based on its status, so I wouldn’t worry with it anymore.

Though if it causes trouble with other people, maybe in the /config/main.php we could have


'loadModelDefaults'=>true

to make the application load the defaults globally, and then scoob.junior’s solution


public $loadDefaults = false;

to override the default values per model and override the global ‘loadModelDefault’, and MadSkillsTisdale’s solution


 public function rules()

        {

                return array(

                        array('somefield', 'numerical'),

                        array('fieldthatNeedsDefault', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => true),

                        array('someotherfield', 'required'),

                        .......

                );

        }

to override the default value per field,override the $loadDefaults per model, and override the ‘loadModelDefaults’ which was declared globally.

Please understand that this thread is old…

This question was asked before the solution was incorporated…

The solution for this problem is


$model->unsetAttributes()

So if you like the defaults you don’t call that method… If you don’t like your defaults… just call $model->unsetAttributes()

Thank you mdomba!

Now it is clear enough! :rolleyes: