Skip to content

Commit

Permalink
update childCount()
Browse files Browse the repository at this point in the history
remove unnecessary scoping when couting by ID
  • Loading branch information
quickapps authored and lorenzo committed Mar 29, 2014
1 parent d7be824 commit ddaeccc
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/Model/Behavior/TreeBehavior.php
Expand Up @@ -85,17 +85,15 @@ public function childCount($id, $direct = false) {
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) {
Expand Down

0 comments on commit ddaeccc

Please sign in to comment.