M
M
maxyc webber2014-06-26 10:04:49
Yii
maxyc webber, 2014-06-26 10:04:49

How to count documents at each level, including sublevels, in a nestedset?

Has anyone asked this question? I think the answers will be useful not only to me.
At the moment, I've only come up with this

private function _getCount($root=true){
        $criteria = new CDbCriteria();
        $criteria->addCondition('parent_id IS '.($root ? '' : ' NOT ').' NULL');
        $criteria->addCondition('category_id IN (
                SELECT
                  id
                FROM
                  forum_categories
                WHERE
                  _left >= :left
                  AND _right<= :right
                  AND _root= :root)'
        );
        $criteria->params[':left']   =   $this->_left;
        $criteria->params[':right']  =   $this->_right;
        $criteria->params[':root']   =   $this->_root;

        return YiiForumPost::model()->count($criteria);
    }

    public function getCountTopics(){
        return $this->_getCount();
    }
    public function getCountPosts(){
        return $this->_getCount(true);
    }

in this example, yii uses the countTopics(), countPosts() getter,
when one of them is called, all branches of the current root are selected. then all documents in these branches are simply considered.
when showing the whole tree, this getter will be called in each line, and this number of requests is equal to the number of branches. which is not very good)
are there any other ideas?
while writing thought up denormalization to carry out and store counters in an additional field of categories.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Solovyov, 2014-06-26
@pavel_salauyou

counters should be stored in the cache (redis), and the base should not be touched

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question