From ea9e91218e362a5abdab054bb7782fe24d71dc26 Mon Sep 17 00:00:00 2001 From: the_undefined Date: Thu, 15 May 2008 03:16:36 +0000 Subject: [PATCH] Fixing bug in Model::saveField, closes #4511 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6877 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 7 ++++++- cake/tests/cases/libs/model/model.test.php | 14 +++++++++++++- cake/tests/cases/libs/model/models.php | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 1d1e95703..5cb93209a 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1011,7 +1011,12 @@ class Model extends Overloadable { * @see Model::save() */ function saveField($name, $value, $validate = false) { - return $this->save(array($this->alias => array($name => $value)), $validate, array($name)); + $id = $this->id; + $this->create(false); + return $this->save(array($this->alias => array( + $this->primaryKey => $id, + $name => $value, + )), $validate, array($name)); } /** * Saves model data to the database. By default, validation occurs before save. diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index a4e071254..d6a755888 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -47,7 +47,8 @@ class ModelTest extends CakeTestCase { 'core.syfile', 'core.image', 'core.device_type', 'core.device_type_category', 'core.feature_set', 'core.exterior_type_category', 'core.document', 'core.device', 'core.document_directory', 'core.primary_model', 'core.secondary_model', 'core.something', 'core.something_else', 'core.join_thing', 'core.join_a', 'core.join_b', 'core.join_c', 'core.join_a_b', 'core.join_a_c', - 'core.uuid', 'core.data_test', 'core.posts_tag', 'core.the_paper_monkies', 'core.person', 'core.underscore_field' + 'core.uuid', 'core.data_test', 'core.posts_tag', 'core.the_paper_monkies', 'core.person', 'core.underscore_field', + 'core.node', 'core.dependency' ); function start() { @@ -1603,6 +1604,17 @@ class ModelTest extends CakeTestCase { $TestModel->id = 1; $result = $TestModel->saveField('title', '', true); $this->assertFalse($result); + + // Test bug #4511 + $this->loadFixtures('Node', 'Dependency'); + $Node =& new Node(); + $Node->set('id', 1); + $result = $Node->read(); + $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); + + $Node->saveField('state', 10); + $result = $Node->read(); + $this->assertEqual(Set::extract('/ParentNode/name', $result), array('Second')); } function testSaveWithCreate() { diff --git a/cake/tests/cases/libs/model/models.php b/cake/tests/cases/libs/model/models.php index be1764e74..c05e88160 100644 --- a/cake/tests/cases/libs/model/models.php +++ b/cake/tests/cases/libs/model/models.php @@ -395,6 +395,20 @@ class NodeNoAfterFind extends CakeTestModel { var $hasMany = array('Child' => array('className' => 'NodeAfterFind', 'dependent' => true)); var $belongsTo = array('Parent' => array('className' => 'NodeAfterFind', 'foreignKey' => 'apple_id')); } +class Node extends CakeTestModel{ + var $name = 'Node'; + var $hasAndBelongsToMany = array( + 'ParentNode' => array( + 'className' => 'Node', + 'joinTable' => 'dependency', + 'foreignKey' => 'child_id', + 'associationForeignKey' => 'parent_id', + ) + ); +} +class Dependency extends CakeTestModel{ + var $name = 'Dependency'; +} class ModelA extends CakeTestModel { var $name = 'ModelA'; var $useTable = 'apples';