Editor - Duplicate action

Editor - Duplicate action

tiago.fernandestiago.fernandes Posts: 13Questions: 4Answers: 0

Hello,

I tried to use the Duplicate button but I currently have a problem with it.
On clicking on Duplicate it shows the error "An SQL error occurred: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '20' for key 'PRIMARY'".
I think this occurs because it's duplicating the key of the row, and I wonder if it's possible to bypass the duplication of this row, and pass it as NULL and then being inserted on the database without showing the error

Duplicate button code:

{
extend: "selected",
                    text: 'Duplicate',
                    action: function ( e, dt, node, config ) {
                        // Start in edit mode, and then change to create
                        editor
                            .edit( table.rows( {selected: true} ).indexes(), {
                                title: 'Duplicate',
                                buttons: 'Duplicate'
                            } )
                            .mode( 'create' );
                    }
},

Controller code

Editor::inst( $db, 'ordens_nacional', 'ordem_nacional' )
    ->readTable("demo")
    ->fields(
        Field::inst( 'ordem_nacional' ),
        Field::inst( 'empresa' ),
        Field::inst( 'data_carga' ),
        Field::inst( 'data_descarga' ),
        Field::inst( 'trator' ),
        Field::inst( 'reboque' ),
        Field::inst( 'cliente' ),
        Field::inst( 'pessoa' ),
        Field::inst( 'tlf' ),
        Field::inst( 'motorista' ),
        Field::inst( 'ref' ),
        Field::inst( 'local_carga' ),
        Field::inst( 'local_descarga' ),
        Field::inst( 'valor' ),
        Field::inst( 'fat' ),
        Field::inst( 'paral' ),
        Field::inst( 'portagens' ),
        Field::inst( 'obs' ),
        Field::inst( 'pago' ),
        Field::inst( 'fat_anulada' ),
        Field::inst( 'vendida_a' ),
        Field::inst( 'vendida_por' ),
        Field::inst( 'user' ),
        Field::inst( 'lc_nome_empresa' ),
        Field::inst( 'lc_morada' ),
        Field::inst( 'lc_cod_postal' ),
        Field::inst( 'lc_localidade' ),
        Field::inst( 'lc_pais' )
            ->options( Options::inst()
                ->table( 'paises' )
                ->value( 'paises' )
                ->label( 'paises' )
            )
            ->validator( Validate::dbValues() ),
        Field::inst( 'ld_nome_empresa' ),
        Field::inst( 'ld_morada' ),
        Field::inst( 'ld_cod_postal' ),
        Field::inst( 'ld_localidade' ),
        Field::inst( 'ld_pais' )
            ->options( Options::inst()
                ->table( 'paises' )
                ->value( 'paises' )
                ->label( 'paises' )
            )
            ->validator( Validate::dbValues() ),
        Field::inst( 'peso_bruto' ),
        Field::inst( 'peso_taxavel' ),
        Field::inst( 'incoterms' )
            ->options( Options::inst()
                ->table( 'transitario_lista_incoterms' )
                ->value( 'id_incoterm' )
                ->label( 'descricao' )
            )
            ->validator( Validate::dbValues() ),
        Field::inst( 'tipo_carga' )
            ->options( Options::inst()
                ->table( 'transitario_lista_modo_carga' )
                ->value( 'id_modo' )
                ->label( 'nome' )
            )
            ->validator( Validate::dbValues() ),
        Field::inst( 'quantidade' ),
        Field::inst( 'medidas' ),
        Field::inst( 'anulada' ),
        Field::inst( 'instrucao' ),
        Field::inst( 'sub_trator' ),
        Field::inst( 'sub_reboque' ),
        Field::inst( 'vendida_ao_cuidado_de_nome' ),
        Field::inst( 'vendida_ao_cuidado_de_numero' ),
        Field::inst( 'vendida_a_fatura_paga' ),
        Field::inst( 'vendida_a_fatura_numero' ),
        Field::inst( 'obs_subcontratado' ),
        Field::inst( 'tipo_transporte' ),
        Field::inst( 'obs_contabilidade' ),
        Field::inst( 'data_correio' ),
        Field::inst( 'statuspag' )
        
    )
    
    ->join(
        Mjoin::inst( 'ficheiros' )
            ->link( 'ordens_nacional.ordem_nacional', 'ordens_files.id_ordem' )
            ->link( 'ficheiros.id', 'ordens_files.file_id' )
            ->fields(
                Field::inst( 'id' )
                    ->upload( Upload::inst('/deployments/3412/uploads/minhascargas/__ID__.__EXTN__' )
                        ->db( 'ficheiros', 'id', array(
                            'filename'    => Upload::DB_FILE_NAME,
                            'filesize'    => Upload::DB_FILE_SIZE,
                            'web_path'    => Upload::DB_WEB_PATH,
                            'system_path' => Upload::DB_SYSTEM_PATH,
                            'username' => 'demo'
                        ) )
                    )
            )
    )
    
    ->process( $_POST )
    ->json();

Sending bellow the debug, and I think it contains all the info necessary
https://debug.datatables.net/ulabas

Does anyone know how to solve this?
Many thanks

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Hi,

    Change:

    Field::inst( 'ordem_nacional' ),
    

    to be:

    Field::inst( 'ordem_nacional' )->set(false),
    

    That will disable Editor from attempting to write to your primary key, leaving it up to the database to generate the new key.

    Allan

Sign In or Register to comment.