How do you set up a last updated function?

How do you set up a last updated function?

koniahinkoniahin Posts: 186Questions: 39Answers: 7

After editing a form I need to have it automatically update a field, last_updated, to the datetime like php now as you click to save. How do you do this?

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    If you are using MySQL, your last_updated field would update automatically if you have defined it correctly.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    In the Editor world, you can also do it in the server-side scripts with Events - see the examples towards the end of this page,

    Colin

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    I looked at the page but javascript especially is not my skillset. With the aid of other searches I did the following. For testing I added a new field called timestamp as:

    Type: Timestamp
    Default: current_timestamp()

    I added the field in the Controller file:

    Field::inst('articles.timestamp') ->set(function ($data) { return date('Y-m-d H:i:s'); }),

    In the javascript file I added the field as:

    {
    label: "Current timestamp (field = timestamp)",
    name: "articles.timestamp",
    type: 'datetime',
    def: function () {
    return new Date(Date.now());
    }
    },

    It is not updating the timestamp which we want to include HH-MM-DD.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    edited March 2023

    You only need the timestamp field in your MySQL, and for queries. You don't want to edit it.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    MySQL should look like this:

    Name ; (your choice of name)
    
    Type: TIMESTAMP
    
    Default: CURRENT_TIMESTAMP
    
    Attributes: on update CURRENT_TIMESTAMP
    

    You should really be looking at MySQL sites. This is not a Datatables issue.

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    What is the command line alter table statement?

    alter table articles change timestamp timestamp ..........

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
  • koniahinkoniahin Posts: 186Questions: 39Answers: 7
    Answer ✓

    My partner solved this by changing the Field in the controller file to:

    Field::inst('articles.modified')->set( Field::SET_EDIT ),

    The field type is date which is fine for what we need. Can you mark this as closed by the poster?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Good to hear that you've got a solution. That's your answer marked as the right one.

    Allan

Sign In or Register to comment.