1

I would like to save user-inputs (Form with multiple text-fields) from the frontend of my component using (admin-) JTable. In this case I would like to save a new comment to the database.

I guess I could do it in the controller (overwrite the save-method) but I guess it would be better if I'd use the model and overwrite the save-method.

Is there any example how to do that?

Best regards :-)

  • The controller would be the proper place to override an existing action. Controllers are your setters, models are your getters if that helps you out in the future. You actions (db saves, redirects, etc.) should always take place in a controller. – Terry Carter Apr 26 '16 at 23:49
  • So I would overwrite the save-Method in my controller and call the JTable, right? – MyFault Apr 27 '16 at 13:14
2

Let me explain how joomla works imo.

Controllers are for creating / executing route and calling model for some work.

In controller you have method called save but it's preprocess $_POST aka $data and pass it to model, $controller->getModel()->save().

For me it's better to call model and pass data and use multiple JTable, it's better for further use and lil' bit of joomla and MVC style.

So in model you can do:

$return = parent::save($data);
if($return) {
    // do something with other post data 
    // JFactory::getApplication()->input->get('jform', array(), 'array);
}

return $return; // ofc if code in statement was good too :-)

now do your work, i dare you! ]:->

  • Hello, I'm working with your method. One question: In frontend I can not use JControllerForm - so I have to create a save-function which calls $this->getModel()->save(); . This works fine - but is it the best way? How can I automatically get $data without calling Factory::getApplication()->input...? – MyFault May 4 '16 at 14:13
  • You need to extend administrator model and view and thats all. – Lanah May 4 '16 at 14:20
  • Hi, I extend my model from JModelAdmin now - but the views are always extended from JViewLegacy...? Well, the model seems to work but the controller does not accept a "save"-method if I do not declare it. – MyFault May 4 '16 at 14:44
  • You need to extend JControllerForm controller in front and add getModel method into it. – Lanah May 5 '16 at 5:24
  • Hello, I added the getModel-Method to my JControllerForm and changed my model to extend from JModelAdmin. If I do not explicitly add the save-method to my controller and trigger $this->getModel()->save($data); the save-Method of the controller is not called. – MyFault May 6 '16 at 10:00

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.