使用joomla db对象的递归函数

时间:2011-08-12 11:57:52

标签: recursion joomla recursive-query

我想在joomla中编写一个递归函数,使用joomla的jmodel的db对象使用类别id获取所有子级别类别。以下是我编写的代码:

    function getChildCategories($type){
            $query = "SELECT id FROM #__cd_categories WHERE parent_id='$type'";
            echo $query."<br/>"; 
            $this->_db->setQuery($query);
            $list = $this->_db->loadObjectList(); 
            if ($this->_db->getErrorNum()) { echo $this->_db->stderr(); return false; }       
            foreach($list as $record){
               $this->childCategories[]= $record->id;
               echo $record->id."<br/>";
               return $this->getChildCategories($record->id);
            }

        return true;            
    }

所以现在的问题是,在joomla中我们使用$ this-&gt; _db_setQuery方法和$ this-&gt; _db-&gt; loadObjectList方法,所以在递归调用结果集时,我认为它会覆盖,我认为因为对象是一样的。那么任何人都可以告诉我如何克服这个问题吗?如果你能用循环来解决这个问题,那对我来说也很有帮助。

我也认为一旦将值分配给$ list变量,那么过写就不应该是问题。所以看起来很奇怪。请告诉我是否可以告诉我如何做到这一点?

提前致谢

1 个答案:

答案 0 :(得分:1)

我认为问题不在于数据库对象被覆盖。自从我一直在努力处理递归函数以来,它已经有点了,但我认为问题在于分配$ list变量。

如果您没有像这样返回该变量而不是布尔值true:

function getChildCategories($type) {
        $query = "SELECT id FROM #__cd_categories WHERE parent_id='$type'";

        $this->_db->setQuery($query);
        $list = $this->_db->loadObjectList(); 
        if ($this->_db->getErrorNum()) { echo $this->_db->stderr(); return false; }  
        if ($list) {
            foreach($list as $record){
               $list->childCategories = $this->getChildCategories($record->id);
            }
            return $list;
        } else {
            return;
        }

}