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.