1

I'm working on SOAP method to update some values in some component.

I need to trigger the save() method from a controller of a particular component, but I'm unable to get an instance of the controller and then call the needed method from this instance in my function.

2
  • Since I'm on my phone I answer this shortly here: instead of calling the component's controller you should call the component's Model directly and then the save() method of that Model. Here in this answer you can find one way of doing that: joomla.stackexchange.com/questions/17101/…
    – Zollie
    Commented Aug 14, 2020 at 17:58
  • Welcome to JSE, please take the tour. Please show us the relevant pieces of code that you are developing. If you are following any tutorials or documentation please provide those links. Commented Aug 14, 2020 at 20:27

1 Answer 1

2

In Joomla when you call a controller's save() method (usually it is a FormController if it has a save() method) the controller's save() method calls the corresponding Model right away and the validate() and save() methods of that Model. Thus when you want to save something using a component's save() method it is better and more straightforward to call the component's Model and the save() method of that Model directly (not via a controller).

In my comment I've already placed an older link about how to include any Model (and it's methods) in your extension, but now here I am giving an updated version of that:

On the top of your file include:

use \Joomla\CMS\MVC\Model\BaseDatabaseModel;

I'm just giving an example of calling the Banner admin Model from com_banners extension of Joomla:

BaseDatabaseModel::addIncludePath($path = JPATH_ADMINISTRATOR . '/components/com_banners/models', $prefix = '');
$myModel = BaseDatabaseModel::getInstance('Banner', 'BannersModel');

$myModel->save($data); // you can save your data

Link to BaseDatabaseModel Class

If you insist on using the Controller then you have to load the Controller class with the help of JLoader class of Joomla. As an example I'm loading the Banner Controller here (from the admin side of Joomla):

JLoader::register("Banner", JPATH_ADMINISTRATOR . '/components/com_banners/controllers/banner.php');
JLoader::load("Banner");
$myControllerClass = new BannersControllerBanner;
// $methods = get_class_methods($myControllerClass); // to check the available methods
// var_dump($methods);

Depending on where you load this class, you can use this with HTMLHelper class methods in your layouts (template files) too.

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.