I'm preparing one of my modules to run with Joomla 4, using the nightly builds. I encountering a problem I didn't have with 3.9. I'm calling the function setState on an article model and get the following exception:

"Call to a member function setState() on boolean"

The $appParams are not empty or anything. Code is also listed below, the line where the exception happens is marked.

// Get an instance of the generic articles model
$model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true)); // This returns false instead of the model!

// Set application parameters in model
$app       = JFactory::getApplication();
$appParams = $app->getParams();
$model->setState('params', $appParams);

// Set the filters based on the module params
$model->setState('filter.published', 1);
$model->setState('filter.article_id', $articleIds);
$model->setState('filter.category_id', $params['eventlist_categories']);

Did I oversee some deprecated function or so? Thanks for your help!

--- Edit ---

I have found that JModelLegacy::getInstance() returns false - so there's probably a new way of how to get the article model in Joomla! 4, right?

1 Answers

1
Zollie On Best Solutions

I am sorry, earlier I gave you an example that I tested only in Joomla 3.9 accidentally and I realized just a bit later that it is not valid anymore in Joomla 4. So now here is the working version which I just tested now in Joomla 4:

$model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Administrator', ['ignore_request' => true]);

Obviously 'Administrator' can be changed to 'Site', depending on which model we want to use.

In Joomla 4 it looks like that MVCFactoryInterface has to be used to create and work with the instances of models.