Editor - i need to be INSERT now() in SQL Database

Editor - i need to be INSERT now() in SQL Database

MbdesignMbdesign Posts: 17Questions: 0Answers: 0

i want to use the Date now() after update an entry, how i can do that?

Field::inst('address_last_modified')
->validator( 'Validate::dateFormat', array(
"empty" => false,
"format" => 'd.m.Y',
"message" => "Bitte folgendes Format yyyy-mm-dd"
) )
->getFormatter( 'Format::date_sql_to_format', 'd.m.Y')
->setFormatter( 'Format::date_format_to_sql', 'd.m.Y')

Replies

  • nigel pasconigel pasco Posts: 37Questions: 6Answers: 0
    edited September 2014

    I have been doing this by defining a variable for the date/time at the top of the php script $date = date('Y-m-d H:i:s', time()); and then posting it to the editor field class using $_POST['data']['timesheet']['entry_date'] = $date;.
    An example from one of my pages below... I hope that helps.
    Nige :P

    <?php
    
    /*
     * Editor server script for DB table person
     * Automatically generated by http://editor.datatables.net/generator
     */
    
    // set variables - to be later replaced by session variables
    $database="project"; //Sets the default database name in bootstrap.php
    date_default_timezone_set('Australia/Melbourne');
    $date = date('Y-m-d H:i:s', time());
    $user_id=$_GET['user'];
    $client_id=$_GET['client'];
    $project_id=$_GET['project'];
    $entry_date = $_GET['date'];
    
    // DataTables PHP library
    include("../../datatables/extensions/Editor-1.3.2/php/DataTables.php");
    
    // Alias Editor classes so they are easy to use
    //TO DO: Add approval process and edited process
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Join,
        DataTables\Editor\Validate;
    
    // Build our Editor instance and process the data coming from _POST
    $editor = Editor::inst( $db, 'timesheet', 'timesheet_id' )
        ->fields(
            Field::inst( 'timesheet.pr_id' )->validator( 'Validate::required' ),
            Field::inst( 'timesheet.totalqty' ),
            Field::inst( 'timesheet.notes' ),
            Field::inst( 'project_resource.resource_id' ),
            Field::inst( 'project_resource.rtipo_id' ),
            Field::inst( 'resources.descript' ),
            Field::inst( 'resources.unit_id' ),
            Field::inst( 'units.descript' ),
            Field::inst( 'rtype.tipo'),
            Field::inst( 'statis.numero' ),
            Field::inst( 'statis.descript' ),
            Field::inst( 'statis.colour' )
    
        )
        ->leftJoin( 'project_resource', 'project_resource.pr_id', '=', 'timesheet.pr_id' )
        ->leftJoin( 'resources', 'resources.resource_id', '=', 'project_resource.resource_id' )
        ->leftJoin( 'units', 'units.unit_id', '=', 'resources.unit_id' )
        ->leftJoin( 'rtype', 'rtype.rtipo_id', '=', 'project_resource.rtipo_id' )
        ->leftJoin( 'statis', 'statis.numero', '=', 'timesheet.statis' )
        ->where( $key = 'timesheet.entry_date', $value = $entry_date, $op = '=' )
        ->where( $key = 'timesheet.project_id', $value = $project_id, $op = '=' )
        ->where( $key = 'timesheet.client_id', $value = $client_id, $op = '=' );
    
    if ( isset($_POST['action']) && $_POST['action'] === 'create' ) {
        // Adding a new record, so add a field that will be written
    
        $editor
        ->fields(
            Field::inst( 'timesheet.timesheet_id' )->set(false),
            Field::inst( 'timesheet.client_id' ),
            Field::inst( 'timesheet.project_id' ),
            Field::inst( 'timesheet.statis' )->set(false),
            Field::inst( 'timesheet.added' )->set(false),
            Field::inst( 'timesheet.addedby_id' ),
            Field::inst( 'timesheet.entry_date' )
        );
        $_POST['data']['timesheet']['client_id'] = $client_id;
        $_POST['data']['timesheet']['project_id'] = $project_id;
        $_POST['data']['timesheet']['addedby_id'] = $user_id;
        $_POST['data']['timesheet']['entry_date'] = $entry_date;
    }
    else {
        // Otherwise editing, deleting or getting data. Just read the owner field
        $editor
            ->fields(
                Field::inst( 'timesheet.timesheet_id' )->set(false),
                Field::inst( 'timesheet.client_id' )->set(false),
                Field::inst( 'timesheet.project_id' )->set(false),
                Field::inst( 'timesheet.statis' ),
                Field::inst( 'timesheet.addedby_id' )->set(false),
                Field::inst( 'timesheet.added' )->set(false),
                Field::inst( 'timesheet.entry_date' )->set(false)
            );
    }
    
    $data = $editor
        ->process($_POST)
        ->data();
    
    if ( !isset($_POST['action']) ) {
        // Get a list of statis for the `select` list
        $data ['statis'] = $db
            ->selectDistinct( 'statis', 'numero as value, descript as label', array('field'=>'timesheets') )
            ->fetchAll();
        $data ['project_resource'] = $db
            ->selectDistinct( 'project_resource', 'pr_id as value, resource_id as label' )
            ->fetchAll();
    }
    // Send it back to the client
     echo json_encode( $data );
    
  • nigel pasconigel pasco Posts: 37Questions: 6Answers: 0

    Please excuse the messy formatting of my answer - I am unsure how to format my posts with proper markdown :(

  • allanallan Posts: 63,356Questions: 1Answers: 10,444 Site admin

    Edited your post to add the formatting. Code formatting rules are documented here :-).

    Allan

This discussion has been closed.