Question

Where and how I am overriding the save method in Joomla 3.0 custom component ?

Current situation:

Custom administrator component.

I have a list view that displays all people stored in table. Clicking on one entry I get to the detailed view where a form is loaded and it's fields can be edited.

On save, the values are stored in the database. This all works fine.However, ....

When hitting save I wish to modify a field before storing it into the database. How do I override the save function and where? I have been searching this forum and googled quiet a bit to find ways to implement this. Anyone who give me a simple example or point me into the right direction ?

Thanks.

Was it helpful?

Solution 3

You can use the prepareTable function in the model file (administrator/components/yourComponent/models/yourComponent.php)

protected function prepareTable($table)
{
    $table->fieldname = newvalue;
}

OTHER TIPS

Just adding this for anyone who wants to know the answer to the question itself - this works if you explicitly wish to override the save function. However, look at the actual solution of how to manipulate values!

You override it in the controller, like this:

/**
 * save a record (and redirect to main page)
 * @return void
 */
function save()
{
    $model = $this->getModel('hello');

    if ($model->store()) {
        $msg = JText::_( 'Greeting Saved!' );
    } else {
        $msg = JText::_( 'Error Saving Greeting' );
    }

    // Check the table in so it can be edited.... we are done with it anyway
    $link = 'index.php?option=com_hello';
    $this->setRedirect($link, $msg);
}

More details here: Joomla Docs - Adding Backend Actions

The prepareTable in the model (as mentioned above) is intended for that (prepare and sanitise the table prior to saving). In case you want to us the ID, though, you should consider using the postSaveHook in the controller:

protected function postSaveHook($model, $validData) {
    $item = $model->getItem();
    $itemid = $item->get('id');
}

The postSaveHook is called after save is done, thus allowing for newly inserted ID's to be used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top