使用 joomla db 对象的递归函数
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题不在于数据库对象被覆盖。自从我一直在与递归函数作斗争以来已经有一段时间了,但我认为问题在于分配 $list 变量。
如果您不返回该变量而不是像这样返回 boolean true :
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: