Automatically filled dates

Automatically filled dates

marwimarwi Posts: 33Questions: 9Answers: 0

Hi

I have many tables with the common two datetime columns "created_at" and "modified_at". Is there any way to let Editor automatically set and update their values?

Maybe it's even possible to use the PHP lib on serverside for this.

regards
Marwi

Replies

  • rf1234rf1234 Posts: 2,983Questions: 87Answers: 421

    the DBMS can handle it for you. For MySQL you would need to define as "Default/Expression"
    a) created_at: CURRENT_TIMESTAMP
    b) modified_at: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

    In that case you don't need to specify the columns in your SQL at all unless you want to display them of course. But then you can do "->set (false)" to avoid Editor doing anything with them and leave the job to the DBMS.

    If you want to do this with Editor PHP:

    Field::inst( 'created_at' )->set(Field::SET_CREATE)
                               ->setValue( mySqlTimestamp() ),
    Field::inst( 'modified_at' )->set(Field::SET_BOTH)
                                ->setValue( mySqlTimestamp() ),
    .................
    function mySqlTimestamp () {
        $obj = new DateTime();
        return $obj->format("Y-m-d H:i:s");
    }
    
This discussion has been closed.