Joomla 2.5 MVC Model prepareTable function

1.1k views Asked by At

while developing a MVC Component, I'm faced with the following problem: Before saving the posted data from the default.php, some data should be revised, if necessary. From what I know so far, the protected Function prepareTable(&$table) in the specific Model should cover my need. I started with a very simple approach, as follows:

protected function prepareTable(&$table){

$table=$this->getTable();

$table->image="HelloWorld";

}

My expectation is, that after submitting the template a specific field in my table has now the value "HelloWorld", but it isn't.

Perhaps, someone could give me an advice how to handle the prepareTable() function?

Thank you

1

There are 1 answers

1
Craig On BEST ANSWER

If everything else is setup correctly the prepareTable(&$table) method already has the table object passed into it.

Generally a prepareTable() in your class wouldn't getTable(), as you replace the $table being passed in which already has the row data bound to it. By replacing it you effectively decouple from the work already done.

I would remove that line your method looks like:

protected function prepareTable(&$table){

    $table->image="HelloWorld";

}

If you look at the simplest implementation of prepareTable() in the Joomla core files, in com_banners you will see something very similar to your method;

/**
 * Prepare and sanitise the table data prior to saving.
 *
 * @param   JTable  A JTable object.
 * @since   1.6
 */
protected function prepareTable(&$table)
{
    $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
}