使用 joomla db 对象的递归函数

发布于 2024-11-29 12:35:37 字数 1018 浏览 1 评论 0原文

我想在 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->; _db_setQuery方法和$this->_db->loadObjectList方法,所以在递归调用结果集时,我认为它会覆盖,我认为因为对象是一样的。那么有人能告诉我如何克服这个问题吗?如果你可以通过使用循环来解决这个问题,那对我来说也是非常有帮助的。

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

提前致谢

I want to write a recursive function in joomla that get all the child level categories using a category id using joomla's jmodel's db object.Following is my code that I have written:

    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;            
    }

So now problem is that, in joomla we use $this->_db_setQuery method and $this->_db->loadObjectList method , so in recursive call the result set, I think it overwrite, I think because the object is same. So can any one tell the way that how to overcome this problem? If you can solve this by using loop even that would be also very helpful for me.

I also think that once values are assigned to $list variable then that over write shouldn't be problem.So seems strange.Please tell if some one can tell me the way to do it?

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2024-12-06 12:35:37

我认为问题不在于数据库对象被覆盖。自从我一直在与递归函数作斗争以来已经有一段时间了,但我认为问题在于分配 $list 变量。

如果您不返回该变量而不是像这样返回 boolean 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;
        }

}

I don't think the issue is with the database object getting overwritten. It has been a bit since I have been struggling with recursive functions but I think the issue is with assigning the $list variable.

Should you not be returning that variable instead of boolean true like this:

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;
        }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文