php - CakePHP: Always use clear() instead of create()? -
there's small, of import difference, between calling clear()
, create()
within loop:
let's assume next code:
foreach ($posts $post) { $this->post->create(); $this->post->id = $post['post']['id']; $this->post->save(['column3' => 'foo', 'column4' => 'bar']); }
when doing create()
: column 1, might default boolean false, magically added updated-fields , can lead info loss (think of post column1 = true).
when doing clear()
instead of create()
: column 1, not mentioned in save-statement not touched @ all.
so, safe rely on clear()
in foreach, existing info partially updated?
second party of question: always improve rely on clear()
? (when looking @ code of clear()
, see, it's convenience wrapper create(false)
). , difference in create()
, create(false)
initialization of default values. think, default values should improve set straight on database-level.
btw: proposed small doc change. sense free +1 this:
so, safe rely on clear() in foreach, existing info partially updated?
yes.
is improve rely on clear()?
the benefit of using create()
when adding records form pre-populated default values shown user. if don't care can utilize clear()
.
php cakephp cakephp-2.5
No comments:
Post a Comment