Can I also update a field while logging?

Can I also update a field while logging?

orionaseliteorionaselite Posts: 49Questions: 13Answers: 4

I have the following

function logChange ( $db, $action, $id, $values ) {
    //var_dump($_SESSION);  
    $username = $_SESSION['username'];
    $desc = 'Admin with email '.$username.' has taken action '.$action.' using values '.json_encode( $values ).' on table ads in row '.$id;
    $db->insert( 'logs', array(
        'user_id'   => $_SESSION['id'],
        'description' => $desc,
        'log_date'   => date('c')
    ) );
}
->on( 'postCreate', function ( $editor, $id, $values, $row ) {
        logChange( $editor->db(), 'create', $id, $values );
    } )
    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
        logChange( $editor->db(), 'edit', $id, $values );
    } )

what I also need to do other then logging successful edits or creates is to set the datetime of a field in my table when either updating or creating. the field name is created_date and updated_date. in my table called ads. They need to be set to the current datetime on create or edit.

ideas?

This question has an accepted answers - jump to answer

Answers

  • orionaseliteorionaselite Posts: 49Questions: 13Answers: 4

    I tried this

    ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
            logChange( $editor->db(), 'create', $id, $values );
            $editor
                ->field( 'created_date' )
                ->setValue(date('c'));
        } )
        ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
            logChange( $editor->db(), 'edit', $id, $values );
            $editor
                ->field( 'updated_date' )
                ->setValue(date('c'));
        } )
    

    I assumed ->setValue(date('c')); would do what I needed but I get 0000-00-00 00:00:00 ideas?

  • orionaseliteorionaselite Posts: 49Questions: 13Answers: 4

    my full code follows

    <?php
    session_start();
    // DataTables PHP library and database connection
    include( "lib/DataTables.php" );
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
    
    function logChange ( $db, $action, $id, $values ) {
        //var_dump($_SESSION);  
        $username = $_SESSION['username'];
        $desc = 'Admin with email '.$username.' has taken action '.$action.' using values '.json_encode( $values ).' on table ads in row '.$id;
        $db->insert( 'logs', array(
            'user_id'   => $_SESSION['id'],
            'description' => $desc,
            'log_date'   => date('c')
        ) );
    }
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'ads', 'id' )
        ->fields(
            Field::inst( 'banner_image' )
                ->validator( 'Validate::notEmpty' )
                ->setFormatter( 'Format::nullEmpty' )
                 //->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'../upload/profile_pics/__ID__.__EXTN__' )
          ->upload(Upload::inst( function ( $file, $id ) {
                move_uploaded_file( $file['tmp_name'], '../../upload/ad_pics/'.$id.'.jpg');
                return $id.'.jpg';
            } )
                    ->db( 'ads_images', 'id', array(
                        'filename'    => Upload::DB_FILE_NAME
                         
                    ) )
                    ->allowedExtensions( [ 'jpg' ], "Παρακαλώ η εικόνα να είναι μορφής .jpg" )
                ),
            Field::inst( 'created_date' )
                ->set( false )
                ->validator( 'Validate::notEmpty' )
                ->validator( 'Validate::dateFormat', array( 'format'=>'Y-m-d H:i:s' ) )
                ->getFormatter( 'Format::datetime', array( 'from'=>'Y-m-d H:i:s', 'to'  =>'d-m-Y H:i:s' ) )
                ->setFormatter( 'Format::datetime', array( 'to'  =>'Y-m-d H:i:s', 'from'=>'d-m-Y H:i:s' ) ),
            Field::inst( 'updated_date' )
                ->set( false )
                ->validator( 'Validate::dateFormat', array( 'format'=>'Y-m-d H:i:s' ) )
                ->getFormatter( 'Format::datetime', array( 'from'=>'Y-m-d H:i:s', 'to'  =>'d-m-Y H:i:s' ) )
                ->setFormatter( 'Format::datetime', array( 'to'  =>'Y-m-d H:i:s', 'from'=>'d-m-Y H:i:s' ) ),
            Field::inst( 'screen_position' )
                ->validator( 'Validate::notEmpty' ),
            Field::inst( 'status' )
                ->validator( 'Validate::notEmpty' )
        )
        ->on( 'postCreate', function ( $editor, $id, $values, $row ) {
            logChange( $editor->db(), 'create', $id, $values );
            $editor
                ->field( 'created_date' )
                ->setValue(date('Y-m-d H:i:s'));
        } )
        ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
            logChange( $editor->db(), 'edit', $id, $values );
            $editor
                ->field( 'updated_date' )
                ->setValue(date('Y-m-d H:i:s'));
        } )
        ->on( 'postRemove', function ( $editor, $id, $values ) {
            logChange( $editor->db(), 'delete', $id, $values );
        } )
        ->where( 'status', 2, '!=' )
        ->process( $_POST )
        ->json();
    

    any ideas why I keep getting 30-11--0001 00:00:00 it's as if my formatters are incorrect

    My db field is of type DATETIME if that helps

  • allanallan Posts: 63,819Questions: 1Answers: 10,517 Site admin
    Answer ✓

    Hi,

    Your use of $editor->field(...)->setValue(...) looks spot on to me - but you have to do that in preEdit! In postEdit, the values have already been written to the database.

    Allan

  • orionaseliteorionaselite Posts: 49Questions: 13Answers: 4

    Thanks although the issue seemed to be becase i decided to exclude the updated_date and created_date from the editor so the Generator had added ->set(false); I just included the fields in the editor and it works fine. It would be nice if I could exclude tthe fields so that the user can't edit them but ok. I can live with that

This discussion has been closed.