From 5de492fb25d441837bdc36f8596acd461faff82a Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 10 Oct 2012 17:21:16 +0530 Subject: [PATCH] Allow saving new records with pre specified primary key value with treebehavior. --- lib/Cake/Model/Behavior/TreeBehavior.php | 2 +- .../Model/Behavior/TreeBehaviorNumberTest.php | 30 +++++++++++++++++ .../Model/Behavior/TreeBehaviorUuidTest.php | 32 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Model/Behavior/TreeBehavior.php b/lib/Cake/Model/Behavior/TreeBehavior.php index eb316a4d7..6895fe9d0 100644 --- a/lib/Cake/Model/Behavior/TreeBehavior.php +++ b/lib/Cake/Model/Behavior/TreeBehavior.php @@ -178,7 +178,7 @@ class TreeBehavior extends ModelBehavior { extract($this->settings[$Model->alias]); $this->_addToWhitelist($Model, array($left, $right)); - if (!$Model->id) { + if (!$Model->id || !$Model->exists()) { if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) { $parentNode = $Model->find('first', array( 'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]), diff --git a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php index d93c6334e..f234a8cce 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php @@ -369,6 +369,36 @@ class TreeBehaviorNumberTest extends CakeTestCase { $this->assertSame($validTree, true); } +/** + * testAddWithPreSpecifiedId method + * + * @return void + */ + public function testAddWithPreSpecifiedId() { + extract($this->settings); + $this->Tree = new $modelClass(); + $this->Tree->initialize(2, 2); + + $data = $this->Tree->find('first', array( + 'fields' => array('id'), + 'conditions' => array($modelClass . '.name' => '1.1') + )); + + $this->Tree->create(); + $result = $this->Tree->save(array($modelClass => array( + 'id' => 100, + 'name' => 'testAddMiddle', + $parentField => $data[$modelClass]['id']) + )); + $expected = array_merge( + array($modelClass => array('id' => 100, 'name' => 'testAddMiddle', $parentField => '2')), + $result + ); + $this->assertSame($expected, $result); + + $this->assertTrue($this->Tree->verify()); + } + /** * testAddInvalid method * diff --git a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorUuidTest.php b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorUuidTest.php index a69445ef9..bd3590b3c 100644 --- a/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorUuidTest.php +++ b/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorUuidTest.php @@ -21,6 +21,7 @@ App::uses('Model', 'Model'); App::uses('AppModel', 'Model'); +App::uses('String', 'Utility'); require_once dirname(dirname(__FILE__)) . DS . 'models.php'; /** @@ -56,6 +57,37 @@ class TreeBehaviorUuidTest extends CakeTestCase { */ public $fixtures = array('core.uuid_tree'); +/** + * testAddWithPreSpecifiedId method + * + * @return void + */ + public function testAddWithPreSpecifiedId() { + extract($this->settings); + $this->Tree = new $modelClass(); + $this->Tree->initialize(2, 2); + + $data = $this->Tree->find('first', array( + 'fields' => array('id'), + 'conditions' => array($modelClass . '.name' => '1.1') + )); + + $id = String::uuid(); + $this->Tree->create(); + $result = $this->Tree->save(array($modelClass => array( + 'id' => $id, + 'name' => 'testAddMiddle', + $parentField => $data[$modelClass]['id']) + )); + $expected = array_merge( + array($modelClass => array('id' => $id, 'name' => 'testAddMiddle', $parentField => '2')), + $result + ); + $this->assertSame($expected, $result); + + $this->assertTrue($this->Tree->verify()); + } + /** * testMovePromote method *