获取文章时出现JModel错误

时间:2012-03-20 21:45:22

标签: joomla joomla-extensions joomla2.5

我正在写一个Joomla 2.5.3模块。我正试图从JModel获得一篇文章。

JModel::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model =& JModel::getInstance('Article', 'ContentModel', array('ignore_request' => true));
$item =& $model->getItem((int) $id);

结果是:

Fatal error: __clone method called on non-object in /var/www/site/joomla/components/com_content/models/article.php on line 170

有谁知道为什么?


修改

/var/www/site/joomla/component/com_content/models/article.php的第170行是

$data->params = clone $this->getState('params');

如果我var_dump($tihs->getState('params')),我会NULL

1 个答案:

答案 0 :(得分:3)

我自己发现了这个错误。代码是这样的:

JModel::addIncludePath(JPATH_SITE.'/components/com_content/models', 'ContentModel');
$model =& JModel::getInstance('Article', 'ContentModel', array('ignore_request'=>true));

$appParams = JFactory::getApplication()->getParams();
$model->setState('params', $appParams);

$item =& $model->getItem($id);

错误是:

Fatal error: __clone method called on non-object in /var/www/site/joomla/components/com_content/models/article.php on line 170

查看类ContentModelArticle的源代码(在components/com_content/models/application.php中)我注意到函数getItem()尝试访问状态对象的变量$params。该变量先前未初始化。

$registry = new JRegistry;
$registry->loadString($data->attribs);
$data->params = clone $this->getState('params'); // 'params' was not previously initialized! The call to clone will fail if you don't provide your own initialization.
$data->params->merge($registry);

上面的代码获取文章的私有属性,然后尝试覆盖全局属性。

在调用$model->getItem($id)之前,我使用以下行向状态对象提供了全局文章属性:

$appParams = JFactory::getApplication()->getParams();
$model->setState('params', $appParams);

现在可行。