2

I'm writing a Joomla 2.5.3 module. I'm trying to get an article from a JModel.

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

The result is:

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

Does anyone know why?


EDIT:

Line 170 of /var/www/site/joomla/component/com_content/models/article.php is

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

If I do var_dump($tihs->getState('params')) I get NULL.

mneri
  • 2,127
  • 2
  • 23
  • 34

1 Answers1

3

I found the error myself. The code is this:

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

The error was:

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

Looking at the source code of class ContentModelArticle (in components/com_content/models/application.php) I noticed that the function getItem() try to access the variable $params of the state object. That variable is not previously initialized.

$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);

The code above gets the article’s private attributes and then tries to override the global attributes.

Before the call to $model->getItem($id) I supplied the global article attributes to the state object with the lines:

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

Now it works.

mneri
  • 2,127
  • 2
  • 23
  • 34