diff --git a/Model/ShimModel.php b/Model/ShimModel.php index cfaf337..6f1ab6d 100644 --- a/Model/ShimModel.php +++ b/Model/ShimModel.php @@ -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 * diff --git a/Test/Case/Model/ShimModelTest.php b/Test/Case/Model/ShimModelTest.php index 75b51a6..f8a5a09 100644 --- a/Test/Case/Model/ShimModelTest.php +++ b/Test/Case/Model/ShimModelTest.php @@ -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 */