2

In my controller of my component, i want to load the model : PluginModel from Joomla\Component\Plugins\Administrator\Model\PluginModel;

In my controller the getModel method return just false, this is my controller :

use Joomla\CMS\MVC\Controller\AdminController;

class PluginsController extends AdminController
{
    public function __construct($config = [])
    {
        parent::__construct($config);
    }

    public function publish()
    {
        $model = $this->getModel();
    }

    public function getModel($name = 'Plugin', $prefix = 'Administrator', $config = ['ignore_request' => true])
    {
        return parent::getModel($name, $prefix, $config);
    }
}

By using this way it works on J!3.9.x but on J!4.0, the $model = $this->getModel(); just return false

How to load the model ?

1 Answer 1

3

Yes, Joomla4 has a different approach. This is how you can call other component's Model:

use Joomla\CMS\Factory;

$app = Factory::getApplication();
$model = $app->bootComponent('com_plugins')->getMVCFactory()->createModel('Plugin', 'Administrator', ['ignore_request' => true]);

or, the legacy way is also working about a year ago in Joomla4:

JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_plugins/models', 'PluginModel');
$model = JModelLegacy::getInstance('Plugin', 'PluginsModel', ['ignore_request' => true]);

I did not test that, but theoretically your code also should work if you add the model path and change the $prefix like:

public function getModel($name = 'Plugin', $prefix = 'PluginsModel', $config = ['ignore_request' => true])
{
    JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_plugins/models', 'PluginModel');
    return parent::getModel($name, $prefix, $config);
}
1
  • 1
    I had tested with the first method and it works for me Aug 7, 2021 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.