Skip to content

Commit

Permalink
Add a helper to migrate away from saveField(): saveFieldById()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Dec 13, 2015
1 parent afd40f2 commit 6493ffe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
23 changes: 22 additions & 1 deletion Model/ShimModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,32 @@ public function fieldByConditions($name, array $conditions = [], array $customOp
*/
public function saveField($name, $value, $validate = false) {
if (Configure::read('Shim.deprecateSaveField')) {
trigger_error('Deprecated in the shim context. Please use save() or updateAll() directly.', E_USER_DEPRECATED);
trigger_error('Deprecated in the shim context. Please use save() or updateAll() directly. saveFieldById() is available as quick fallback.', E_USER_DEPRECATED);
}
return parent::saveField($name, $value, $validate);
}

/**
* Saves the value of a single field to the database, based on the current
* model ID.
*
* @param int $id Id
* @param string $field Name of the table field
* @param mixed $value Value of the field
* @param bool|array $validate Either a boolean, or an array.
* If a boolean, indicates whether or not to validate before saving.
* If an array, allows control of 'validate', 'callbacks' and 'counterCache' options.
* See Model::save() for details of each options.
* @return bool|array See Model::save() False on failure or an array of model data on success.
*/
public function saveFieldById($id, $field, $value, $validate = false) {
$data = [
'id' => $id,
$field => $value
];
return $this->save($data, ['validate' => $validate]);
}

/**
* Updates the counter cache of belongsTo associations after a save or delete operation
*
Expand Down
17 changes: 17 additions & 0 deletions Test/Case/Model/ShimModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,23 @@ public function testDeleteAllRaw() {
$this->assertSame(1, $result);
}

/**
* @return void
*/
public function testSaveFieldById() {
$data = [
'user' => 'Fooo'
];
$result = $this->User->save($data);
$this->assertTrue((bool)$result);

$this->User->saveFieldById($result['User']['id'], 'user', 'Baar');
$this->assertTrue((bool)$result);

$result = $this->User->get($result['User']['id']);
$this->assertSame('Baar', $result['User']['user']);
}

/**
* @return void
*/
Expand Down

0 comments on commit 6493ffe

Please sign in to comment.