Browse Source

Adding a method to ensure the entities have the tree columsn present

Jose Lorenzo Rodriguez 12 years ago
parent
commit
6d5a870d05

+ 30 - 0
src/Model/Behavior/TreeBehavior.php

@@ -157,6 +157,7 @@ class TreeBehavior extends Behavior {
 	protected function _setParent($entity, $parent) {
 		$config = $this->config();
 		$parentNode = $this->_getNode($parent);
+		$this->_ensureFields($entity);
 		$parentLeft = $parentNode->get($config['left']);
 		$parentRight = $parentNode->get($config['right']);
 		$right = $entity->get($config['right']);
@@ -215,6 +216,7 @@ class TreeBehavior extends Behavior {
 	protected function _setAsRoot($entity) {
 		$config = $this->config();
 		$edge = $this->_getMax();
+		$this->_ensureFields($entity);
 		$right = $entity->get($config['right']);
 		$left = $entity->get($config['left']);
 		$diff = $right - $left;
@@ -296,6 +298,7 @@ class TreeBehavior extends Behavior {
 				->count();
 		}
 
+		$this->_ensureFields($node);
 		return ($node->{$right} - $node->{$left} - 1) / 2;
 	}
 
@@ -386,6 +389,7 @@ class TreeBehavior extends Behavior {
  */
 	public function removeFromTree(Entity $node) {
 		return $this->_table->connection()->transactional(function() use ($node) {
+			$this->_ensureFields($node);
 			return $this->_removeFromTree($node);
 		});
 	}
@@ -442,6 +446,7 @@ class TreeBehavior extends Behavior {
  */
 	public function moveUp(Entity $node, $number = 1) {
 		return $this->_table->connection()->transactional(function() use ($node, $number) {
+			$this->_ensureFields($node);
 			return $this->_moveUp($node, $number);
 		});
 	}
@@ -516,6 +521,7 @@ class TreeBehavior extends Behavior {
  */
 	public function moveDown(Entity $node, $number = 1) {
 		return $this->_table->connection()->transactional(function() use ($node, $number) {
+			$this->_ensureFields($node);
 			return $this->_moveDown($node, $number);
 		});
 	}
@@ -722,4 +728,28 @@ class TreeBehavior extends Behavior {
 
 		return $query;
 	}
+
+/**
+ * Ensures that the provided entity contains non-empty values for the left and
+ * right fields
+ *
+ * @param \Cake\ORM\Entity $entity The entity to ensure fields for
+ * @return void
+ */
+	protected function _ensureFields($entity) {
+		$config = $this->config();
+		$fields = [$config['left'], $config['right']];
+		$values = array_filter($entity->extract($fields));
+		if (count($values) === count($fields)) {
+			return;
+		}
+
+		$fresh = $this->_table->get($entity->get($this->_table->primaryKey()), $fields);
+		$entity->set($fresh->extract($fields), ['guard' => false]);
+
+		foreach ($fields as $field) {
+			$entity->dirty($field, false);
+		}
+	}
+
 }

+ 14 - 0
tests/TestCase/Model/Behavior/TreeBehaviorTest.php

@@ -107,6 +107,20 @@ class TreeBehaviorTest extends TestCase {
 	}
 
 /**
+ * Tests that childCount will provide the correct lft and rght values
+ *
+ * @return void
+ */
+	public function testChildCountNoTreeColumns() {
+		$table = $this->table;
+		$node = $table->get(6);
+		$node->unsetProperty('lft');
+		$node->unsetProperty('rght');
+		$count = $this->table->childCount($node, false);
+		$this->assertEquals(4, $count);
+	}
+
+/**
  * Tests the childCount() plus callable scoping
  *
  * @return void