1

I am developing Joomla 3.4 application where I have to call one component model into another component controller but not call from there.

Support, i have 2 component

>> comp1
  model: m1
  controller: c1
>> comp2
  model: m2
  controller: c2

I want to call comp1 model (m1) into comp2 controller (c2).

I tried using below code:

$model    = $this->getModel('m1', '', array());

But in $model get null value if above code use in comp1 controller (c1) then run perfect.

What actually issue is not getting. Any one have a perfect idea.

Thanks

harsh4u
  • 2,550
  • 4
  • 24
  • 39

2 Answers2

5

This is an old question but better late than never and I hope it will help other developers.

To call a model from another component you need firstly to include the path of this model:

JModelLegacy::addIncludePath(JPATH_SITE . '/components/comp1/models', 'Comp1Model');

Secondly you have to create an instance of your model:

$model = JModelLegacy::getInstance('Model1', 'Comp1Model');

After that you should be able to use the methods of your model.

Munto
  • 61
  • 3
0

Joomla 4 has a excellent dependency injection for this. Old technique not worked for me.

I use this

//frontend component Atricle model loaded
$model = Factory::getApplication()->bootComponent('com_content')->getMVCFactory()->createModel('Article', 'Site');

$result = $model->getItem('put ID here');
Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68