Skip to content
This repository
Browse code

Allow saving new records with pre specified primary key value with tr…

…eebehavior.
  • Loading branch information...
commit 5de492fb25d441837bdc36f8596acd461faff82a 1 parent 4acc687
ADmad authored October 10, 2012
2  lib/Cake/Model/Behavior/TreeBehavior.php
@@ -178,7 +178,7 @@ public function beforeSave(Model $Model) {
178 178
 		extract($this->settings[$Model->alias]);
179 179
 
180 180
 		$this->_addToWhitelist($Model, array($left, $right));
181  
-		if (!$Model->id) {
  181
+		if (!$Model->id || !$Model->exists()) {
182 182
 			if (array_key_exists($parent, $Model->data[$Model->alias]) && $Model->data[$Model->alias][$parent]) {
183 183
 				$parentNode = $Model->find('first', array(
184 184
 					'conditions' => array($scope, $Model->escapeField() => $Model->data[$Model->alias][$parent]),
30  lib/Cake/Test/Case/Model/Behavior/TreeBehaviorNumberTest.php
@@ -370,6 +370,36 @@ public function testAddMiddle() {
370 370
 	}
371 371
 
372 372
 /**
  373
+ * testAddWithPreSpecifiedId method
  374
+ *
  375
+ * @return void
  376
+ */
  377
+	public function testAddWithPreSpecifiedId() {
  378
+		extract($this->settings);
  379
+		$this->Tree = new $modelClass();
  380
+		$this->Tree->initialize(2, 2);
  381
+
  382
+		$data = $this->Tree->find('first', array(
  383
+			'fields' => array('id'),
  384
+			'conditions' => array($modelClass . '.name' => '1.1')
  385
+		));
  386
+
  387
+		$this->Tree->create();
  388
+		$result = $this->Tree->save(array($modelClass => array(
  389
+			'id' => 100,
  390
+			'name' => 'testAddMiddle',
  391
+			$parentField => $data[$modelClass]['id'])
  392
+		));
  393
+		$expected = array_merge(
  394
+			array($modelClass => array('id' => 100, 'name' => 'testAddMiddle', $parentField => '2')),
  395
+			$result
  396
+		);
  397
+		$this->assertSame($expected, $result);
  398
+
  399
+		$this->assertTrue($this->Tree->verify());
  400
+	}
  401
+
  402
+/**
373 403
  * testAddInvalid method
374 404
  *
375 405
  * @return void
32  lib/Cake/Test/Case/Model/Behavior/TreeBehaviorUuidTest.php
@@ -21,6 +21,7 @@
21 21
 
22 22
 App::uses('Model', 'Model');
23 23
 App::uses('AppModel', 'Model');
  24
+App::uses('String', 'Utility');
24 25
 require_once dirname(dirname(__FILE__)) . DS . 'models.php';
25 26
 
26 27
 /**
@@ -57,6 +58,37 @@ class TreeBehaviorUuidTest extends CakeTestCase {
57 58
 	public $fixtures = array('core.uuid_tree');
58 59
 
59 60
 /**
  61
+ * testAddWithPreSpecifiedId method
  62
+ *
  63
+ * @return void
  64
+ */
  65
+	public function testAddWithPreSpecifiedId() {
  66
+		extract($this->settings);
  67
+		$this->Tree = new $modelClass();
  68
+		$this->Tree->initialize(2, 2);
  69
+
  70
+		$data = $this->Tree->find('first', array(
  71
+			'fields' => array('id'),
  72
+			'conditions' => array($modelClass . '.name' => '1.1')
  73
+		));
  74
+
  75
+		$id = String::uuid();
  76
+		$this->Tree->create();
  77
+		$result = $this->Tree->save(array($modelClass => array(
  78
+			'id' => $id,
  79
+			'name' => 'testAddMiddle',
  80
+			$parentField => $data[$modelClass]['id'])
  81
+		));
  82
+		$expected = array_merge(
  83
+			array($modelClass => array('id' => $id, 'name' => 'testAddMiddle', $parentField => '2')),
  84
+			$result
  85
+		);
  86
+		$this->assertSame($expected, $result);
  87
+
  88
+		$this->assertTrue($this->Tree->verify());
  89
+	}
  90
+
  91
+/**
60 92
  * testMovePromote method
61 93
  *
62 94
  * @return void

0 notes on commit 5de492f

Please sign in to comment.
Something went wrong with that request. Please try again.