public function counts($table, $match, $id = null)
    {
        if (!is_null($id)) {
            return (int) $this->db->value(array(
                'SELECT COUNT('.$table.'.'.$match.') AS count',
                'FROM '.$this->table.' AS node',
                'INNER JOIN '.$this->table.' AS parent',
                'INNER JOIN '.$table,
                'WHERE parent.'.$this->id.' = '.(int) $id.' AND node.lft BETWEEN parent.lft AND parent.rgt AND node.'.$this->id.' = '.$table.'.'.$match,
                'GROUP BY parent.'.$this->id,
                'ORDER BY parent.lft',
            ));
        } else {
            $counts = array();
            if ($stmt = $this->db->query(array(
                'SELECT parent.'.$this->id.', COUNT('.$table.'.'.$match.') AS count',
                'FROM '.$this->table.' AS node',
                'INNER JOIN '.$this->table.' AS parent',
                'INNER JOIN '.$table,
                'WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.'.$this->id.' = '.$table.'.'.$match,
                'GROUP BY parent.'.$this->id,
                'ORDER BY parent.lft',
            ))) {
                while (list($id, $count) = $this->db->fetch($stmt)) {
                    $counts[$id] = (int) $count;
                }
                $this->db->close($stmt);
            }

            return $counts;
        }
    }