Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2726: ActiveRecord now fills default values on creating new instance of the model if defaults are available from DB schema #2815

Merged
merged 3 commits into from Mar 19, 2014

Conversation

@samdark
Copy link
Member

@samdark samdark commented Mar 19, 2014

Pros of this approach:

  • No need to manually fill values.
  • Defaults could be changed w/o regenerating model code.
  • Consistent with how DB works.
…stance of the model if defaults are available from DB schema
@samdark
Copy link
Member Author

@samdark samdark commented Mar 19, 2014

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 19, 2014

This is not correct because it may overwrite the values that are set via constructor.

I suggest we introduce a method loadDefaultValues(). Users should explicitly call this method if they want to respect default values. The usage is mainly in inserting a new record.

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 19, 2014

Two more issues:

  • If a user explicitly specifies some columns to return in a query (e.g. in REST API), he may find in a surprise that he gets default values in unwanted columns (and even worse is that the values could be incorrect.)
  • Performance issue: affect the performance of querying with many records
@samdark
Copy link
Member Author

@samdark samdark commented Mar 19, 2014

Are we actually setting any values via constructor?

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 19, 2014

The framework doesn't, but it's totally valid to do $user = new User(['username' => 'test']);

@samdark
Copy link
Member Author

@samdark samdark commented Mar 19, 2014

In a rest API it will get real values from data source, not default ones.

@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 19, 2014

In a rest API it will get real values from data source, not default ones.

Not the case with this PR. Imagine I want to return a user list with only id and username in the result. With this PR, other irrelevant fields will be filled with default values, which is both unexpected (unexpected fields) and incorrect (the values may be incorrect).

In summary, setting default values is only useful when inserting new records. And even in this case, some users may not like prefilling with default values.

@samdark
Copy link
Member Author

@samdark samdark commented Mar 19, 2014

Makes sense. Changed it to be separate method so the syntax is:

$model = new MyModel();
$model->loadDefaultValues()->save();
@qiangxue
Copy link
Member

@qiangxue qiangxue commented Mar 19, 2014

Looks good to me now. Potential future enhancement is to allow loading default values for specific attributes. Probably not needed for now.

samdark added a commit that referenced this pull request Mar 19, 2014
Fixes #2726: ActiveRecord now fills default values on creating new instance of the model if defaults are available from DB schema
@samdark samdark merged commit 922c5da into master Mar 19, 2014
@samdark samdark deleted the ar-default-values branch Mar 19, 2014
@Sammaye
Copy link

@Sammaye Sammaye commented Mar 19, 2014

If this is run by a function then say I was to do as @qiangxue says with: $user = new User(['username' => 'test']); how would I then apply defaults without potentially removing that custom value?

@samdark
Copy link
Member Author

@samdark samdark commented Mar 19, 2014

You can't. Use individual assignments after calling loadDefaultValues() instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.