-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
don't forget to call clear() instead of create() #1440
Conversation
this makes a difference!
I think the docs should continue to recommended using create(). I think getting the defaults is useful when creating new records. I can see why clear() could be better for updates though. |
We truly have a problem now with this behavior since we have big amount of data overwritten by this behavior. I started a question on Stack overflow concerning this and my currently unanswered question is: Isn't it always better to use clear() in the code and have the database deal with default-values? I don't see any point, why cakePHP itself tries to set those (based on the database-schema). (it would be different, if you have the possibility to programmatically set default-values on a per-model level though) |
Prefilling default values in model can help populate forms with those values and showing to user. |
@ADmad really good point, although that seems unrelated to me in regards to the underlying db-save-operation. |
Yes, for your use case when editing records using |
I'm on @johannesnagl side. I've stumbled upon that case plenty times and when doing loops you never wan't defaults to be put in. create() is only useful for an edit action |
edit? You mean add, right? @lorenzo |
Ok, then lets update the docs to suggest clear(). |
don't forget to call clear() instead of create()
@dereuromark yes, sorry |
thx guys! :) |
@johannesnagl Thanks for suggesting the change :) |
don't forget to call clear() instead of create()
don't forget to call clear() instead of create()
we had a problem in one of our apps:
let's assume the following code:
foreach ($posts as $post) {
$this->Post->create();
$this->Post->id = $post['Post']['id'];
$this->Post->save(['column3' => 'foo', 'column4' => 'bar']);
}
When calling create() instead of clear(), cakePHP saves default values to columns, which are not part of the save()-call. (e.g. 'column1' defaults to boolean false, but can have changed to "true" due to another save-operation).
When calling clear(), cakePHP saves only the mentioned columns.