I am trying to find the preferred way to save form data using joomla API methods. I am trying to save data with an ajax-call. First i tried just calling the components standard save method, calling:

index.php?option=com_whatever&view=wdata&task=save // ...etc

This works, sort of, but I cannot find a way to verify the call actually worked.

Now I'm trying to use the model functions from the new and very nice ajax-plugin in joomla 3.3, but I'm unsure what is the right approach. I can do something like this:

function onAjaxSaveWData(){
    if(!JSession::checkToken()) return 0; // checking the token
    $data=$in->get('jform',array(),'ARRAY'); //this is unfiltered input?!
    JLoader::register('WhateverModelWData', $whatevermodelpath);
    $model = new WhateverModelWData();

The only problem with this approach is it's not working. The save function fails with an error:

Call to a member function getKeyName() on a non-object in libraries/legacy/model/admin.php on line 1064

In addition to this not working, it would have not filtered the data before insertion.

I would think $model->validate($model->getForm(), $data) would do the trick, but also $model->getForm() fails miserably, so I'm stuck. Can anyone help me out?

  • There's a similar question that has several good answers that you should have a look at: joomla.stackexchange.com/questions/146/…
    – GDP
    Commented Aug 15, 2014 at 13:14
  • Thanks, GDP, but the referenced question all expects that I want to change the component that I'm querying. This is not the case: I only want to use the available mvc-functions (if possible) to save data. So the referenced $model - object could be ContentModelArticle, which I don't want to modify, only use standard and available functionality to save article data.
    – jonasfh
    Commented Aug 15, 2014 at 13:37
  • Ah...further explanation clarifies that - I had been trying to do something similar, and the referenced question solved my scenario.
    – GDP
    Commented Aug 15, 2014 at 13:40
  • Thus, the answer to the question is given in this link: joomla.stackexchange.com/questions/146/…
    – Anibal
    Commented Aug 16, 2014 at 19:27

1 Answer 1


Initially I figured the best would be to use the controller method save(), which I finally got working like this:

$path = JPATH_ADMINISTRATOR . '/components/com_anycomponent;
JLoader::register('AnycomponentControllerAnyController', $path . '/controllers/anycontroller.php');
$controller = AnycomponentControllerAnyController::getInstance('Anycomponent'); 
$controller->addModelPath($path . '/models', 'Anycomponent');  
JTable::addIncludePath($path . '/tables'); 
JForm::addFormPath($path . '/models/forms');
$ret = $controller->save();

So, when calling a controller from outside the component, you need to specify all paths that's needed. This applies when the controller extends JControllerForm, which is now in the legacy folder in joomla, but used throughout the joomla core. For frontend - controllers it might not work so smooth, unless these also extend JControllerForm.

When calling save like this, all necessary checks seems to be done: data-validation, access-checks etc.

I still don't see the best way to get the inserted id from the controller, the save() function simply returns true or false depending on the success of the function call.

It is possible to use the same method as above for calling model -save() (my suggestion in the question above), adding the neccessary paths first. This can be more flexible, however then you need to perform data-validation-checks and access-checks yourself. Calling controller-save() takes care of these issues.

Please feel free to correct/extend this answer if you have more info on the subject.

Edit: If you are using this method to save existing data, you need to add the id-parameter to the url, like:

// where 20 is the id you are editing

If the id is only in a field on the form this will be ignored, and the record added as a new record.

  • This works well for me - managed to get the record id from the save method by overriding the postSaveHook method on JControllerFormin the child controller: protected function postSaveHook(JModelLegacy $model, $validData = array()){ $item = $model->getItem(); $id = $item->get('id'); ... } Commented Apr 26, 2016 at 10:02

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.