php - Joomla 2.5 父类别 ID

标签 php joomla joomla2.5

我正在尝试获取另一个类别的父类别的 id。 (我有子类别的 ID。)

我一直在尝试使用 Joomla 的类别模型,我成功地使用了文章等效项从文章的 Id 中获取子类别 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;

最佳答案

如果你想加载ContentModelCategory,你最好使用前缀:

$categoriesModel = JModel::getInstance('Category','ContentModel');

但是,我认为这不是适合您的模型,因为它从请求中获取上下文。

我可以建议 2 个替代方案:

使用 JCategories 实例

这是内容组件使用的首选方法。

$article = $articlesModel->getItem($id);
$catid = $article->catid;

//Get Category Parent Category
$categoriesModel = JCategories::getInstance('content');
$category = $categoriesModel->get($catid);
$parent = $category->getParent();
if($parent){
    $parentId = $parent->id;
    echo "parentId: ".$parentId;
}

使用CategoriesModelCategory

这是 com_categories 使用的模型。

$article = $articlesModel->getItem(5);
$catid = $article->catid;

//add the CategoriesModelCategory model include path 
JModel::addIncludePath (JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_categories' . DS . 'models');
//add the table include path required by the model
JTable::addIncludePath (JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_categories' . DS . 'tables');

$categoriesModel = JModel::getInstance('Category','CategoriesModel');
$category = $categoriesModel->getItem($catid);
if($category){
    $parentId = $category->parent_id;
    echo "parentId: ".$parentId;
}

关于php - Joomla 2.5 父类别 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18870748/

相关文章:

php - 如何在 Windows 10 中更新 Composer

joomla - Joomla:视频结尾处如何停止在jv_youtube中显示相关视频

php - 在php中转义单引号mysql查询

mysql - MySQL 查询中的乘法(Joomla、php)

php - 使用 Ajax 查询的 joomla 组件开发

php - 选择 date=NOW()?

php - 匹配不区分大小写的带空格的精确短语

Joomla 3.2安装错误: Table usergroups doesn't exist

joomla - 基于语言的安装描述

php - 为什么 PHP Composer 这么慢?