Browse Source

update childCount()

remove unnecessary scoping when couting by ID
QuickApps 12 years ago
parent
commit
ddaecccd1d
1 changed files with 6 additions and 8 deletions
  1. 6 8
      src/Model/Behavior/TreeBehavior.php

+ 6 - 8
src/Model/Behavior/TreeBehavior.php

@@ -85,17 +85,15 @@ class TreeBehavior extends Behavior {
 		list($parent, $left, $right) = [$config['parent'], $config['left'], $config['right']];
 
 		if ($direct) {
-			$query = $this->_table->find()
-				->where([$parent => $id]);
-			return $this->_scope($query)->count();
+			$count = $this->_table->find()
+				->where([$parent => $id])
+				->count();
+			return $count;
 		}
 
-		$node = $this->_table->find()
-			->select([$parent, $left, $right])
-			->where([$this->_table->primaryKey() => $id]);
-		$node = $this->_scope($node)->first();
+		$node = $this->_table->get($id, [$this->_table->primaryKey() => $id]);
 
-		return $node->{$right} - $node->{$left} - 1;
+		return ($node->{$right} - $node->{$left} - 1) / 2;
 	}
 
 	protected function _scope($query) {