Is there some docs about the correlation between yii model creation and database structure? I mean, if a field is NOT NULL I want that yii put that field required in the model rules and if the field have a default values I want that Yii in the beforeValidate check if the field is empty and set the default values that I typed in the database like default column value.
I use the NOT NULL attribute in another table on a character varying column and when I create the model it doesn't put in the rules() that field like 'required'.
I'm afraid this was a change to Yii in response to a suggestion from me. My thinking was that, if there is a default, then the user does not really need to add his own value–assuming, of couse, that the default really is an acceptable and usable value. (A default value of "New user name" is not such a value.) A value may be required by the database, but is not required from the user. It has become my habit to mark ALL fields NOT NULL and supply a default only when I do not require a value from my end user.
Okay, that's one plan. I don't really have an argument to claim it is better than yiic model assuming that all NOT NULL fields should be required.
Maybe there should be a poll as to what Yii'ers prefer yiic model to do??? Whatever the plan, it will be suboptimal for some programmers and not others. I have no illusions about my way of working is objectively better than anyone else's.
When I create the model of User yii create the .php putting in the rules() function that 'Name' it's required but it doesn't use the Default value settings defined in the DB.
With something like
$user = new User()
$user gets filled with the default values that can then be seen and changed (or not) in the create view. Is this not working for you?
if a field is required I want that yii impost it required in the model and that's ok.
If there's no default value it's ok that yii throw an error and say to the user: "that field cannot be blank becouse it's required".
The problem comes when in the db the field is defined like NOT NULL and has a default value. If yii doesn't use the beforeValidate to put the default value into the attribute of the object ( that it's required ), yii will throw the blank error message becouse that field ( that it's required ) it's blank and the DB default value information it's not used.
I don't want to use save(false), and I think that this is the best way to follow:
if the field is NOT NULL yii have to put it as required.
if the field have a default value but it's not null, yii can create a model without the beforeValidate rule for that field.
if the field is both NOT NULL and has a default value in the DB yii has to create the model with a beforeValidate method for that field putting the default value of that field into the object attribute if the value is empty.
I know that I can’t explain well my thought in english, but I hope that you understand what I was trying to say
All these are controlled by the validation rules on the Yii side. In general, the yiic tool may generate appropriate rules to reflect the fact some columns are required (NOT NULL). But do not count on the generated code. You should adjust it based on your requirements.
The same also applies to the default values, which is even more complex because some databases allow using DB expressions to be default values. There's no way that PHP can handle that. In this case, you should declare public members with default values, or set default values before you save the record if needed.