【发布时间】:2023-03-16 08:24:01
【问题描述】:
我正在尝试获取另一个类别的父类别的 ID。 (我有子类别的 ID。)
我一直在尝试使用 Joomla 的 Category 模型,我成功地使用 Article 等效项从文章的 Id 中获取 Child Category ID。
//article model
jimport('joomla.application.component.model');
$articlesModel = JModel::getInstance('ContentModelArticle');
$categoriesModel = JModel::getInstance('ContentModelCategory');
//Get Article Category id
$article = $articlesModel->getItem($art['id']);
$catid = $article->catid;
//Get Category Parent Category
$category = $categoriesModel->getItem($catid);
$parentID = $category->getParent();
echo "<pre>";
var_dump($parentID);
echo "</pre>";
但我不断收到错误消息,提示我正在尝试调用非对象的函数。
谁能指出我哪里出错了?谢谢。
编辑:应该提到这都是在模块文件中
改变战术 最后,我自己找到了另一种方法来做到这一点: 现在我正在查询数据库以获取信息。在这种情况下它很有用,因为我可以获取我需要的确切数据。
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('parent_id');
$query->from('#__categories');
$query->where("id = '$child_id'");
$db->setQuery($query);
//check if error
if ($db->getErrorNum()) {
echo $db->getErrorMsg();
exit;
}
$parent = $db->loadObjectList();
$parent_id = $parent['0']->parent_id;
【问题讨论】:
-
确切的错误是什么?