How to Override the Default Save Method in a Joomla Component

Joomla is great when it comes to facilitating things for developers. For example, if you want to create a form that saves and retrieves data from the database in the backend, all you need to do is to create the XML schema file and create some standard files (these files are merely copy and paste from standard components, with minor modifications) and Joomla does the rest.

However, Joomla assumes that the data inputted by the user in the backend is the same that you want saved in the database – which is not always the case. In some cases, you need to manipulate the data a bit before saving it.

For example, one of our current projects consists of migrating a very large component from Joomla 1.5 to Joomla 2.5. One of the challenges that we faced was that a calendar field in one of the forms had to be saved as a UNIX timestamp field in the database, and then retrieved back as a calendar field. This meant that we had to manipulate the data when saving it into the database and when retrieving it from the database.

So, how did we manipulate the data prior to saving it to the database?

It was a bit hard at first because the documentation about Joomla’s pre-save functionality is very scarce – but then we discovered it. There is a function called prepareTable (this function is declared in the file modeladmin.php [line 821] which is located under the libraries/joomla/application/component folder and is invoked from the function save [line 1000] in that same file) that is called immediately prior to saving the data to the database. The prepareTable function takes the current form as parameter, and inside this function you can do all the data manipulation that you want. Here’s what we did in our scenario:

protected function prepareTable($form)
    $form->expired = strtotime($form->expired);

Note that the above function has to be placed in the model of the form that you’re trying to override – in our case, it had to be in the file subscription.php located under the administrator/components/com_store/models directory.

And how did we manipulate the data prior to displaying it in the form?

Well, that was extremely easy. All that we needed to do was adding a line to the function loadFormData which was also in the model file subscription.php. The function became as follows:

protected function loadFormData() 
    // Check the session for previously entered form data.
    $data = JFactory::getApplication()->getUserState('', array());
    if (empty($data)) 
        $data = $this->getItem();
    //now we need to modify the data here a bit
    $data->expired = date("Y-m-d", $data->expired);
    return $data;

That was it!

Now – if you’re reading this because you’re stuck then we hope our post was helpful. If this wasn’t the case, or if you need some Joomla experts to take over the development of your custom component, then look no further. Just contact us and we’ll do the work for you professionally, swiftly, and for a very affordable cost.

One Response to “How to Override the Default Save Method in a Joomla Component”
  1. Comment by Javier Eraso — March 28, 2014 @ 10:34 am

    Great post !

    You saved my day. Couldn’t find how to accomplish this prior to read your article.


Leave a comment