1

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.

4
  • 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.) – mickmackusa May 26 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 – Huub S May 26 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. – mickmackusa May 26 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 May 27 at 3:57
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.

mycomponent/controller/messages.php

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.

0
$model     = $this->getModel('name');
$getMethod = $model->myFunction($someParams);
1
  • 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. – mickmackusa May 26 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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