Skip to content
Permalink
Browse files

Always update updated/modified columns when a fieldList is used.

When a fieldList is used, and updated is not in the fieldList, the
column should continue to be updated even if the column has a value from
the user. Because the field is not in the fieldList, we must assume that
the intent is for the field to update automatically, as it would have if
the updated column was not present in the save data.

Refs #7076
  • Loading branch information...
markstory committed Sep 6, 2015
1 parent 0dbabce commit 60d7bbaa104f5c67d521efb26200c70be0dd8ff5
Showing with 32 additions and 3 deletions.
  1. +6 −1 lib/Cake/Model/Model.php
  2. +26 −2 lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -1835,7 +1835,12 @@ protected function _doSave($data = null, $options = array()) {
$now = time();
foreach ($dateFields as $updateCol) {
if (in_array($updateCol, $fields) || !$this->hasField($updateCol)) {
$fieldHasValue = in_array($updateCol, $fields);
$fieldInWhitelist = (
count($this->whitelist) === 0 ||
in_array($updateCol, $this->whitelist)
);
if (($fieldHasValue && $fieldInWhitelist) || !$this->hasField($updateCol)) {
continue;
}
@@ -387,6 +387,30 @@ public function testSaveFieldListResetsWhitelistOnFailedSave() {
$this->assertEquals($whitelist, $model->whitelist);
}
/**
* Test that save() with a fieldList continues to write
* updated in all cases.
*
* @return void
*/
public function testSaveUpdatedWithFieldList() {
$this->loadFixtures('Post', 'Author');
$model = ClassRegistry::init('Post');
$original = $model->find('first', ['conditions' => ['Post.id' => 1]]);
$data = array(
'Post' => array(
'id' => 1,
'title' => 'New title',
'updated' => '1999-01-01 00:00:00',
)
);
$model->save($data, array(
'fieldList' => ['title']
));
$new = $model->find('first', ['conditions' => ['Post.id' => 1]]);
$this->assertGreaterThan($original['Post']['updated'], $new['Post']['updated']);
}
/**
* Test save() resets the whitelist after afterSave
*
@@ -1960,8 +1984,8 @@ public function testSaveHabtm() {
'title' => 'New Article With Tags and fieldList',
'body' => '',
'published' => 'N',
'created' => '',
'updated' => ''
'created' => static::date(),
'updated' => static::date(),
),
'Tag' => array(
0 => array(

0 comments on commit 60d7bba

Please sign in to comment.
You can’t perform that action at this time.