Table not updating when adding new row / editing row with custom primary key

Table not updating when adding new row / editing row with custom primary key

gcacgcac Posts: 21Questions: 1Answers: 0

I'm using Editor 1.6 and Datatables and have a very simple table / source. Using PHP and server side. I have used the Editor::new and specified the primary key (which is not auto-increment, its a unique serial number) in the third parameter according to the docs.

When I add a new row via the Editor pop-up, the new row is not shown and debugging shows an empty data[] returned. When I edit a row, it updates fine as long as I don't touch the primary key field. Relevant code segments below. Hopefully this is something easily fixed. I don't need an extra "id" field in the table since the umpid field is always unique.

Editor::inst( $db , 'hardware', 'umpid')
    ->field(
        Field::inst( 'hardware.name' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'hardware.wifi_mac' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'hardware.wired_mac' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'hardware.serialnum' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'hardware.umpid' )->validator( 'Validate::notEmpty' ),
        Field::inst( 'hardware.notes' ),
        Field::inst( 'hardware.updated')
            ->setValue( date('Y-m-d H:i:s') ),
        Field::inst( 'hardware.updated_by' )
            ->setValue( $_SESSION['display_name'] )
    )
    ->process($_POST)
    ->json();
var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
    editor = new $.fn.dataTable.Editor( {
        ajax: "ajax_hardware_data.php",
        table: "#hardwareData",
        fields: [ {
                label: "Name:",
                name: "hardware.name"
            }, {
                label: "WiFi Address (MAC):",
                name: "hardware.wifi_mac"
            }, {
                label: "Ethernet Address (MAC)",
                name: "hardware.wired_mac"
            }, {
                label: "Serial Number:",
                name: "hardware.serialnum"
            }, {
                label: "UM Property ID#",
                name: "hardware.umpid"
            },{
                label: "Notes:",
                name: "hardware.notes"
            } 
        ]
    } );
 
    $('#hardwareData').DataTable( {
        dom: "lBfrtipl",
        ajax: {
            url: "ajax_hardware_data.php",
            type: 'POST'
        },
        columns: [
            { data: "hardware.name" },
            { data: "hardware.wifi_mac" },
            { data: "hardware.wired_mac" },
            { data: "hardware.serialnum" },
            { data: "hardware.umpid" },
            { data: "hardware.notes" }
            ],
        select: true,
        "lengthMenu": [ [20, 25, 50, -1], [20, 25, 50, "All"] ],
        buttons: [
        <?php if (chkgrp("IT Staff")) : ?>
            { extend: "create", editor: editor },
            { extend: "remove", editor: editor },
        <?php endif; ?>
            { extend: "edit",   editor: editor }
        ]
    } );
    
} );

Replies

  • allanallan Posts: 61,886Questions: 1Answers: 10,140 Site admin

    Interesting - thanks for posting this. Could you try it without the hardware. prefix. I suspect it will work there.

    I'll take a closer look into what is causing this and try to reproduce it here. If you are able to try it without the prefix, could you let me know how it goes?

    Allan

  • gcacgcac Posts: 21Questions: 1Answers: 0

    Allan, thanks for the prompt reply.

    I took the table prefixes out of both ajax_hardware_data.php and the main page in the editor definition and it did indeed fix the issue.

    I had been always using the table prefixes since I had seen those in examples and had also had other issues when using multiple tables with joins and such.

    This is a simple interface and at this point can work just fine with this single table. My only concern is if we add more complexity (joins) if it will continue to work.

    Thanks for a great product and great support. Datatables and Editor sure make my life easier :-)

    Nate

This discussion has been closed.