1

In my custom component I have two database tables for items in my component with the standard 'created' and 'modified' datetime columns to contain timestamps when those items are created or modified.

I initially assumed that the easiest way to do this was via the database, setting the default value for 'created' as current_timestamp and the 'modified' column to be set with current_timestamp on update. This worked fine on my local install (XAMPP) which is on MariaDB 10.4.28, but when I updated my component on my Siteground test site which uses MySQL 5.7.44 it throws an error. Datetime columns don't seem to like the current_timestamp value. One possible fix that I saw suggested was to change datetime to timestamp but it made no difference.

I then looked at how core Joomla tables with 'created' and 'modified' columns are set up and they don't use current_timestamp to automatically populate. It looks as if I need to get my code to set these values instead.

That being the case, I added 'created' and 'modified' to my form XML and brought them them into my edit.php template file. I could set the timestamp value when I first open a new or existing item, but ideally I'd like to do it immediately prior to saving. Is there a javascript event that I can watch for when the Save button is clicked that I can make use of? If not, is it a case of extending the FormController Save PHP function and setting the values there?

2 Answers 2

1

Using the Banners component as an example you can see the way that it handles the empty date fields in the ..\administrator\components\com_banners\src\Table\BannerTable.php check() method.

The check() method is called by the save() method of the ..\libraries\src\Table\Table.php. The save() method calls bind(), check(), store() and checkIn() methods.

In the Model for your Form you would have it call the save() method found in AdminModel or your own Table object.

/**
* Overloaded check function
*
* @return  boolean
*
             * @see     Table::check
* @since   1.5
*/
public function check()
{
...
    // Set created date if not set.
    if (!(int) $this->created) {
    $this->created = Factory::getDate()->toSql();
    }
    ...
    // Set modified to created if not set
    if (!$this->modified) {
    $this->modified = $this->created;
    }
            
    // Set modified_by to created_by if not set
    if (empty($this->modified_by)) {
    $this->modified_by = $this->created_by;
    }
...
}
1
  • Thanks - that's perfect. All working nicely now :)
    – Grant Hood
    Jan 25 at 8:59
1

Do your database tables have associated classes which extend the Joomla Table class?

If so, you can set these attributes in a store() function, overriding Table::store(). They'll then be set just before saving to the database.

You can see how com_content does this by looking at libraries/src/Table/Content.php

1
  • Thanks Robbie - I've gone with setting the timestamps in the check function as per Irata's reply, but as I haven't really looked at my table classes since setting them up when I first started building my component it's been a good exercise to get more familiar with them. It's opened up a few ways where I can now improve some of the other code I've written.
    – Grant Hood
    Jan 25 at 9:05

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.