Editor creates new entry on edit

Editor creates new entry on edit

ee2018ee2018 Posts: 1Questions: 1Answers: 0

I have a strange behaviour of datatables editor: I use DataTables and the editor with serverside data. Creating and editing data is working fine in nearlly all cases. Just in one case I have the problem, that whenever I edit an entry, it gets not only modified but also a new one is created. So the edit process works fine, but for some reason the create process gets fired up?

Maybe someone has an idea.

The editor fields are defined:

```
<?php
// DataTables PHP library
include( "../php/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\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;

// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'ee_cashflow', 'cashflowID' )
->debug( true )
->fields(
Field::inst( 'cashflowID' )->set(false),
Field::inst( 'caseNr' ),
Field::inst( 'subject' ),
Field::inst( 'brutto' )
->getFormatter( function($val, $data, $field) {return str_replace ( '.' , ',' , $val );})
->setFormatter( function($val, $data, $field) {return str_replace ( ',' , '.' , $val );}) ,
Field::inst( 'dateT' )
->getFormatter( function ( $val, $data, $opts ) {
if($val != NULL){
return date( 'd.m.Y', strtotime( $val ) );
}
return NULL;
})
->setFormatter( function ( $val, $data, $opts ) {
if($val != NULL){
return date( 'Y.m.d', strtotime( $val ) );
}
return NULL;
}),
Field::inst( 'hon' ),
Field::inst( 'emp' )
)
->process( $_POST )
->json();

if ( isset( $_POST['action'] ) && $_POST['action'] === 'edit' ) {
    $cashflows = $_POST['data'];
    $updates = array();
    foreach ($cashflows as $cashflow){
        $keys = array_keys($cashflow); 
        foreach ($keys as $key) {
            if($key == 'brutto'){
                $updates[$key] = str_replace ( ',' , '.' , $cashflow[$key] );
            }
            else{
                $updates[$key] = $cashflow[$key];
            }
        }
    }
    $where = array('cashflowID' => substr(key($cashflows),9));
    $db->update( 'ee_cashflow', $updates, $where );   
}
<?php > ``` ?>

Answers

  • allanallan Posts: 63,195Questions: 1Answers: 10,412 Site admin

    Could you change this part:

        ->process( $_POST )
        ->json();
    

    to be:

        ->debug(true)
        ->process( $_POST )
        ->json();
    

    Then trigger an edit action from the browser and look at the "Network" tab in your browser's inspector. For the request that was sent for that edit, click on it and then the "Response" tab and it will show was the server sent back, which will include the SQL Editor executed. Can you then paste that here so I can take a look please?

    One thing - I don't understand the need for the block of code from line 45. That looks like it is attempting to do an edit itself?

    Allan

This discussion has been closed.