set last update time in php when cell changes

set last update time in php when cell changes

dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

I'm sure there is a really elegant way to have the PHP set the time() whenever a write to any field in a row occurs.

I have a field of type datetime in mysql that is only in the Editor fields and not in the dataTable object.

There is the section in the example code showing how to format it on read.

Field::inst( 'tablename.lastupdated')
   ->set( false )
   ->getFormatter( 'Format::date_sql_to_format', 'jS F Y' )

but I'm wondering where to do the write to tablename.lastupdated

somehow in the
->process( $_POST )
->data();
block ?

thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Hi,

    If you are using the 1.4 libraries (which are currently in beta, but at a feature for exactly this situation!) you can use:

    Field::inst( 'tablename.lastupdated')
      ->setValue( date('c') )
      ->getFormatter( 'Format::date_sql_to_format', 'jS F Y' )
    

    i.e. the value to be used when setting is given by setValue().

    With the 1.3 libraries, it is possible, but a bit more complicated as you need to modify the $_POST array when submitting the value:

    if ( isset( $_POST['action'] ) && ( $_POST['action'] == 'create' || $_POST['action'] == 'remove' ) ) {
      $_POST['data']['tablename.lastupdated'] = date('c');
    }
    

    Hopefully you will agree that the 1.4 method is much easier :-).

    The other thing to note is that you have set( false ) at the moment. That will stop Editor ever writing to this field, which doesn't sound like what you want.

    Allan

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

    Great to hear that it will be so easy.

    I have dataTables.editor.js 1.4.0 beta but get invalid json because of
    Call to undefined method DataTables\Editor\Field::setValue()

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    It sounds like you might have the 1.4 beta Javascript, but possibly not the PHP files. Might that be correct? The setValue() method is defined in the Field.php file.

    Allan

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

    I have the mysql field set to DATETIME as setValue is definitely writing a new value each time the row is touched - but with and without the getFormatter call I always get null as the value returned.

    How can I easiest debug the php code to see why ?

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin

    Hi,

    One option is to use getFormatter with an anonymous function that logs the data to a file:

    getFormatter( function ( $val, $data, $opts ) {
      file_put_contents( "/tmp/a", "Value: $val\n" FILE_APPEND );
      return $val;
    } )
    

    that won't do any formatting, but it will log what has been read from the database to the file /tmp/a which you can tail -f watch as you load the page.

    Allan

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

    hmmmm the setVal() value is definitely getting there to the DB, and the getFormatter is showing null for $val so it looks like reading it back is not working.

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

    I'm sure there is a really elegant way to have the PHP set the time() whenever a write to any field in a row occurs.

    I think you should be using MySQL's timestamp rather than datetime. That way you don't need to write to that field at all.

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

    Well dare I say it but - show me the code - does anyone have a working set of configuration for this feature. Nothing seems to work for me.

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    If you remove the get formatter, what happens? Do you get the SQL ISO8601 timestamp or something else?

    Allan

  • dp@ii.netdp@ii.net Posts: 38Questions: 10Answers: 0

    There was a bug in DT that is now fixed so this item can be closed.

This discussion has been closed.