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

don't forget to call clear() instead of create() #1440

Merged
merged 1 commit into from
Jun 24, 2014

Conversation

johannesnagl
Copy link
Contributor

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.

@markstory
Copy link
Member

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.

@johannesnagl
Copy link
Contributor Author

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)

@ADmad
Copy link
Member

ADmad commented Jun 24, 2014

Prefilling default values in model can help populate forms with those values and showing to user.

@johannesnagl
Copy link
Contributor Author

@ADmad really good point, although that seems unrelated to me in regards to the underlying db-save-operation.

@ADmad
Copy link
Member

ADmad commented Jun 24, 2014

Yes, for your use case when editing records using clear() makes sense.

@lorenzo
Copy link
Member

lorenzo commented Jun 24, 2014

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

@dereuromark
Copy link
Member

edit? You mean add, right? @lorenzo

@markstory
Copy link
Member

Ok, then lets update the docs to suggest clear().

markstory added a commit that referenced this pull request Jun 24, 2014
don't forget to call clear() instead of create()
@markstory markstory merged commit 16660b2 into cakephp:master Jun 24, 2014
@lorenzo
Copy link
Member

lorenzo commented Jun 24, 2014

@dereuromark yes, sorry

@johannesnagl
Copy link
Contributor Author

thx guys! :)

@ADmad
Copy link
Member

ADmad commented Jun 24, 2014

@johannesnagl Thanks for suggesting the change :)

cake17 added a commit that referenced this pull request Jun 24, 2014
don't forget to call clear() instead of create()
@johannesnagl johannesnagl deleted the patch-1 branch March 21, 2017 08:09
tenkoma added a commit to tenkoma/docs that referenced this pull request Mar 15, 2024
don't forget to call clear() instead of create()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants