2

I create a new component for Joomla and just work on an override for the save() method which is inherited from the JModelAdmin class.

The goal is to modify the data a bit before I submit it to the database to ensure some validations.

However, I wonder how I can get the "original" data from the AdminViewElement before I edited it. I would like to compare the two values and only if there's a difference, I would like to do something.

I had a look at some other components to get a feeling for it, but I've no idea where I can get this data from so I can compare the old and new value.

Here's the start of the method before I start with the modelling...

public function save($data)
{
    $input  = JFactory::getApplication()->input;
    $filter = JFilterInput::getInstance();
1
  • Welcome to JSE, Lord. Please take our tour. Commented Jan 22, 2021 at 21:59

1 Answer 1

2

The JModelAdmin save() method handles both inserting a single record into a table and updating a single record, and it calls the following Table-related functions:

$table = $this->getTable();

This enables it to get an instance of the Table class - eg for com_content it will be the Content class in libraries/src/Content/Table.php. I'm assuming that you have an equivalent table class for your component to enable CRUD operations on your database table.

It then works out what is the primary key of the table, and checks the $data array to see if that array element is set. If it is, then it loads the existing record from the database using

$table->load($pk);

At this stage the Table class instance has the field values of the existing table in memory.

After that it calls:

$table->bind($data)

which has the effect in the Table class instance of overwriting the existing values with the new values from the $data array.

Then it calls

$table->check()

to perform any validation checks on the new data. And then

$table->store()

to save the record to the database.

So if you've followed this paradigm when you've implemented your Table class, then maybe the best place to do what you want is within the bind() method of your Table class (although you can do it in the save() before you call bind() where the existing data is available via eg $table->id). At that stage you have the existing record field values available through eg $this->id and then new data available in the $data array passed in as a parameter. If you're using an id field in your database table then checking if $this->id is zero or not will tell you whether it's handling an INSERT or an UPDATE.

You may find the description in the Basic Table API Guide useful.

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.