I need to get data from another model than the view specific controller/model into the view. But I'm not sure if the way I'm doing it now is correct.

I do that via the following way in the view.html.php file:

JModelLegacy::addIncludePath(JPATH_SITE . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . 'com_mycomponent' . DIRECTORY_SEPARATOR . 'models');

//get instance of model class, where class name will be fooModelBar
$model = JModelLegacy::getInstance('Messages', 'MyModel');

//call model method
$this->messages = $model->getData();

But I have a feeling this is not the "correct" way to handle things. Can anyone educate me where I actually have to put this code? I know I am probably not doing it correctly by adding the model to the view.html.php file, but I am a bit confused where I need to add it. I tried adding a display() method to the profile controller but that didn't do anything.

  • By posting resolving advice as an edit, your question becomes a code review. This is perfectly okay, but I recommend that you edit the question again to remove the initial / failed coding attempt. Only showing the working code and purely asking for refining advice will make your question leaner and more attractive to read. Your title, too, can be improved -- specify that you are seeking "best practices". (We even have a tag for best-practices.) Commented May 26, 2021 at 12:50
  • Okay, sorry I wasn't aware of that, I just came up with the solution while I asked the question before.. I will try to edit the question and make it a code review instead of a question
    – Kuubs
    Commented May 26, 2021 at 12:54
  • No worries, I'm just trying to help. You have done the right thing by continuing to pursue a working solution. I prefer to have my own code reviewed / validated so that I can learn the best techniques and also learn if I am making any glaring mistakes. Commented May 26, 2021 at 13:09
  • Is the View displaying data from two or more models, i.e. the normal view model and the 'messages' model in your example?
    – Irata
    Commented May 27, 2021 at 3:57

2 Answers 2


My approach would be to update your code to move away from JModelLegacy and by placing the addIncludePath in the subcontroller using the following lines.


Use Joomla\CMS\MVC\Model\BaseDatabaseModel;
.    .    .
BaseDatabaseModel::addIncludePath(JPATH_SITE . '/components/com_mycomponent/models');
$this->msgsModel = BaseDatabaseModel::getInstance('Messages', 'MyModel');

Alternatively you can use $this->addModelPath from the BaseController class that your controller may already extend, which issues the above addIncludePath statement, see /libraries/src/MVC/Controller/BaseController at line 143. However the addIncludePath statement may not actually be required if the model directory is already in the path of your component.

By setting the $this->msgsModel in the subcontroller you should be able to call the model in the sub controller, another Model, a Helper, other code file or View(although probably not recommended under MVC rules) easily.

//call model method
$this->messages = $this->msgsModel->getData();

The View should only access $this->messages to get the data as it should not be concerned with how or where $this->messages is created.

Ignoring the comment about FooModelBar I would say you need to create a file called /component/com_mycomponent/models/messages.php with a class name of MyModelMessages to hold the code of your Model. Because you aren't following the Joomla naming convention so that it can be automagically resolved, you can use whatever replacement you choose for MyModel.

  • Is this still considered best practice now that Joomla 4 has released? I noticed that the comments in the legacy model wanted the use of MVCFactory as a replacement.
    – Chaosxmk
    Commented Sep 7, 2021 at 21:01
  • 1
    @Chaosxmk I believe it is still an acceptable way to add a model, whether it is Best Practice is very subjective. Only the addIncludePath is depreciated in Joomla 5, BaseDatabaseModel and addModelPath are not flagged as being depreciated in the foreseeable future. I am not familiar enough with Joomla4 yet to know if an alternative or improved method is available.
    – Irata
    Commented Sep 8, 2021 at 0:38
  • alright, well I guess for now it's best to proceed with this until the documentation for Joomla 4 is done and more time has been had using it.
    – Chaosxmk
    Commented Sep 8, 2021 at 11:41
$model     = $this->getModel('name');
$getMethod = $model->myFunction($someParams);
  • Your answer has been flagged by the system as being low-quality. Please be more generous when you post. Please always explain every snippet that you post so that the OP and thousands of future researchers not only receive a solution, but an education as well. Commented May 26, 2021 at 22:22

Your Answer

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

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